from models import *
from django.template import Context, loader
from django.http import HttpResponse
from django.utils import simplejson as json
import trans,langs,specials
import traceback
from core.settings import PAGE_SIZE


def getItemList(request):
    try:
        pageIndex = int(request.GET.get("pageIndex",1))
        startIndex = (pageIndex -1)* PAGE_SIZE
        endIndex = startIndex + PAGE_SIZE
        allrecords = Item.objects.all().order_by('-id')
        pageLength = (allrecords.count() + PAGE_SIZE -1)/ PAGE_SIZE
        items = allrecords[startIndex:endIndex]
        items = items[:]
        for item in items:
            lang = langs.getlangById(item.name)
            if lang:
                item.name = lang.value
            lang = langs.getlangById(item.desc)
            if lang:
                item.desc = lang.value
            item.categoryId = item.categoryId
            item.typeId = item.typeId
            item.attackType = item.attackType
            item.level = item.level
            item.qualityId = item.qualityId
            item.enchantTime = item.enchantTime
            item.bindType = item.bindType
            item.props = item.props
            item.propertySet = item.propertySet
            item.requiredLevel = item.requiredLevel
            item.requiredCareerId = item.requiredCareerId
            item.requiredGenderId = item.requiredGenderId
            item.unique = item.unique
            item.maxStack = item.maxStack
            item.recyclingPrice = item.recyclingPrice
            item.canSell = item.canSell
            item.skillId = specials.getSkillName(item.skillId)
            item.itemres = item.itemres
                
        _context = Context({
            'items':items,
            'pageIndex':pageIndex,
            'pageLength':pageLength
        })
    
        _template = loader.get_template('item/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def getItemJsonList(request):
    try:
        items = Item.objects.all().order_by('-id')
        items = items[:]
        for item in items:
            lang = langs.getlangById(item.name)
            if lang:
                item.name = lang.value
            lang = langs.getlangById(item.desc)
            if lang:
                item.desc = lang.value
            item.categoryId = item.categoryId
            item.typeId = item.typeId
            item.attackType = item.attackType
            item.level = item.level
            item.qualityId = item.qualityId
            item.enchantTime = item.enchantTime
            item.bindType = item.bindType
            item.props = item.props
            item.propertySet = item.propertySet
            item.requiredLevel = item.requiredLevel
            item.requiredCareerId = item.requiredCareerId
            item.requiredGenderId = item.requiredGenderId
            item.unique = item.unique
            item.maxStack = item.maxStack
            item.recyclingPrice = item.recyclingPrice
            item.canSell = item.canSell
            item.skillId = item.skillId
            item.itemres = item.itemres
                
        itemJsonList = []
        for item in items:
            itemJson = trans.item2Json(item)
            itemJsonList.append(itemJson)
        return HttpResponse(json.dumps({'code':1,'result':itemJsonList}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def searchItem(request):
    try:
        condition = request.GET.get('condition')
        value = request.GET.get('value')
        searchArg = {condition:value}
        items = Item.objects.filter(**searchArg).all().order_by('-id')
        for item in items:
            lang = langs.getlangById(item.name)
            if lang:
                item.name = lang.value
            lang = langs.getlangById(item.desc)
            if lang:
                item.desc = lang.value
            item.categoryId = item.categoryId
            item.typeId = item.typeId
            item.attackType = item.attackType
            item.level = item.level
            item.qualityId = item.qualityId
            item.enchantTime = item.enchantTime
            item.bindType = item.bindType
            item.props = item.props
            item.propertySet = item.propertySet
            item.requiredLevel = item.requiredLevel
            item.requiredCareerId = item.requiredCareerId
            item.requiredGenderId = item.requiredGenderId
            item.unique = item.unique
            item.maxStack = item.maxStack
            item.recyclingPrice = item.recyclingPrice
            item.canSell = item.canSell
            item.skillId = item.skillId
            item.itemres = item.itemres
                
        _context = Context({
            'items':items,
            'pageLength':1
        })
    
        _template = loader.get_template('item/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def addItem(request):
    try:
        item = Item()
        item.deployId = 0;
        item.name = request.POST.get('name')         
        item.desc = request.POST.get('desc')         
        item.categoryId = request.POST.get('categoryId')         
        item.typeId = request.POST.get('typeId')         
        item.attackType = request.POST.get('attackType')         
        item.level = int(request.POST.get('level'))        
        item.qualityId = request.POST.get('qualityId')         
        item.enchantTime = int(request.POST.get('enchantTime'))        
        item.bindType = request.POST.get('bindType')         
        item.props = request.POST.get('props')         
        item.propertySet = request.POST.get('propertySet')         
        item.requiredLevel = int(request.POST.get('requiredLevel'))        
        item.requiredCareerId = request.POST.get('requiredCareerId')         
        item.requiredGenderId = request.POST.get('requiredGenderId')         
        item.unique = request.POST.get('unique')         
        item.maxStack = int(request.POST.get('maxStack'))        
        item.recyclingPrice = int(request.POST.get('recyclingPrice'))        
        item.canSell = request.POST.get('canSell')         
        item.skillId = request.POST.get('skillId')         
        item.itemres = request.POST.get('itemres')                 
        item.save()
        item.deployId = item.id + ModelTypes.Type_Item * 1000000;
        lang = langs.setlang(ModelTypes.Type_Item,item.deployId,'name',item.name)
        item.name = lang.id
        lang = langs.setlang(ModelTypes.Type_Item,item.deployId,'desc',item.desc)
        item.desc = lang.id
        item.save()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')
    

def removeItem(request):
    try:
        itemId = int(request.GET.get('itemId'))
        item = Item.objects.get(id=itemId)
        if item :
            langs.dellangById(item.name)
            langs.dellangById(item.desc)
            item.delete()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def getItem(request):
    try:
        itemId = int(request.GET.get('itemId'))
        item = Item.objects.get(id=itemId)
        lang = langs.getlangById(item.name)
        if lang :
            item.name = lang.value
        lang = langs.getlangById(item.desc)
        if lang :
            item.desc = lang.value
                
        itemJson = trans.item2Json(item)
        
        if item :
            return HttpResponse(json.dumps({'code':1,'result':itemJson}), mimetype='application/json')
        else:
            return HttpResponse(json.dumps({'code':0,'message':'cannot find item!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def editItem(request):
    try:
        itemId = int(request.POST.get('itemId'))
        item = Item.objects.get(id=itemId)
        if item :
            langs.setlangById(item.name,request.POST.get('name'))
            
            langs.setlangById(item.desc,request.POST.get('desc'))
            
            item.categoryId = request.POST.get('categoryId')
            item.typeId = request.POST.get('typeId')
            item.attackType = request.POST.get('attackType')
            item.level = int(request.POST.get('level'))
            
            item.qualityId = request.POST.get('qualityId')
            item.enchantTime = int(request.POST.get('enchantTime'))
            
            item.bindType = request.POST.get('bindType')
            item.props = request.POST.get('props')
            item.propertySet = request.POST.get('propertySet')
            item.requiredLevel = int(request.POST.get('requiredLevel'))
            
            item.requiredCareerId = request.POST.get('requiredCareerId')
            item.requiredGenderId = request.POST.get('requiredGenderId')
            item.unique = request.POST.get('unique')
            item.maxStack = int(request.POST.get('maxStack'))
            
            item.recyclingPrice = int(request.POST.get('recyclingPrice'))
            
            item.canSell = request.POST.get('canSell')
            item.skillId = request.POST.get('skillId')
            item.itemres = request.POST.get('itemres')            
            item.save()
            return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
        else :
            return HttpResponse(json.dumps({'code':0,'message':'cannot find item!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

    
def getSkillList(request):
    try:
        pageIndex = int(request.GET.get("pageIndex",1))
        startIndex = (pageIndex -1)* PAGE_SIZE
        endIndex = startIndex + PAGE_SIZE
        allrecords = Skill.objects.all().order_by('-id')
        pageLength = (allrecords.count() + PAGE_SIZE -1)/ PAGE_SIZE
        skills = allrecords[startIndex:endIndex]
        skills = skills[:]
        for skill in skills:
            lang = langs.getlangById(skill.name)
            if lang:
                skill.name = lang.value
            lang = langs.getlangById(skill.desc)
            if lang:
                skill.desc = lang.value
            skill.category = skill.category
            skill.targetType = skill.targetType
            skill.damage = skill.damage
            skill.triggerDamage = skill.triggerDamage
            skill.status = skill.status
            skill.triggerStatus = skill.triggerStatus
            skill.trigger = skill.trigger
            skill.property = skill.property
            skill.changeFury = skill.changeFury
            skill.sound = skill.sound
            skill.skillres = skill.skillres
                
        _context = Context({
            'skills':skills,
            'pageIndex':pageIndex,
            'pageLength':pageLength
        })
    
        _template = loader.get_template('skill/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def getSkillJsonList(request):
    try:
        skills = Skill.objects.all().order_by('-id')
        skills = skills[:]
        for skill in skills:
            lang = langs.getlangById(skill.name)
            if lang:
                skill.name = lang.value
            lang = langs.getlangById(skill.desc)
            if lang:
                skill.desc = lang.value
            skill.category = skill.category
            skill.targetType = skill.targetType
            skill.damage = skill.damage
            skill.triggerDamage = skill.triggerDamage
            skill.status = skill.status
            skill.triggerStatus = skill.triggerStatus
            skill.trigger = skill.trigger
            skill.property = skill.property
            skill.changeFury = skill.changeFury
            skill.sound = skill.sound
            skill.skillres = skill.skillres
                
        skillJsonList = []
        for skill in skills:
            skillJson = trans.skill2Json(skill)
            skillJsonList.append(skillJson)
        return HttpResponse(json.dumps({'code':1,'result':skillJsonList}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def searchSkill(request):
    try:
        condition = request.GET.get('condition')
        value = request.GET.get('value')
        searchArg = {condition:value}
        skills = Skill.objects.filter(**searchArg).all().order_by('-id')
        for skill in skills:
            lang = langs.getlangById(skill.name)
            if lang:
                skill.name = lang.value
            lang = langs.getlangById(skill.desc)
            if lang:
                skill.desc = lang.value
            skill.category = skill.category
            skill.targetType = skill.targetType
            skill.damage = skill.damage
            skill.triggerDamage = skill.triggerDamage
            skill.status = skill.status
            skill.triggerStatus = skill.triggerStatus
            skill.trigger = skill.trigger
            skill.property = skill.property
            skill.changeFury = skill.changeFury
            skill.sound = skill.sound
            skill.skillres = skill.skillres
                
        _context = Context({
            'skills':skills,
            'pageLength':1
        })
    
        _template = loader.get_template('skill/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def addSkill(request):
    try:
        skill = Skill()
        skill.deployId = 0;
        skill.name = request.POST.get('name')         
        skill.desc = request.POST.get('desc')         
        skill.category = request.POST.get('category')         
        skill.targetType = request.POST.get('targetType')         
        skill.damage = request.POST.get('damage')         
        skill.triggerDamage = request.POST.get('triggerDamage')         
        skill.status = request.POST.get('status')         
        skill.triggerStatus = request.POST.get('triggerStatus')         
        skill.trigger = request.POST.get('trigger')         
        skill.property = request.POST.get('property')         
        skill.changeFury = int(request.POST.get('changeFury'))        
        skill.sound = request.POST.get('sound')         
        skill.skillres = request.POST.get('skillres')                 
        skill.save()
        skill.deployId = skill.id + ModelTypes.Type_Skill * 1000000;
        lang = langs.setlang(ModelTypes.Type_Skill,skill.deployId,'name',skill.name)
        skill.name = lang.id
        lang = langs.setlang(ModelTypes.Type_Skill,skill.deployId,'desc',skill.desc)
        skill.desc = lang.id
        skill.save()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')
    

def removeSkill(request):
    try:
        skillId = int(request.GET.get('skillId'))
        skill = Skill.objects.get(id=skillId)
        if skill :
            langs.dellangById(skill.name)
            langs.dellangById(skill.desc)
            skill.delete()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def getSkill(request):
    try:
        skillId = int(request.GET.get('skillId'))
        skill = Skill.objects.get(id=skillId)
        lang = langs.getlangById(skill.name)
        if lang :
            skill.name = lang.value
        lang = langs.getlangById(skill.desc)
        if lang :
            skill.desc = lang.value
                
        skillJson = trans.skill2Json(skill)
        
        if skill :
            return HttpResponse(json.dumps({'code':1,'result':skillJson}), mimetype='application/json')
        else:
            return HttpResponse(json.dumps({'code':0,'message':'cannot find skill!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def editSkill(request):
    try:
        skillId = int(request.POST.get('skillId'))
        skill = Skill.objects.get(id=skillId)
        if skill :
            langs.setlangById(skill.name,request.POST.get('name'))
            
            langs.setlangById(skill.desc,request.POST.get('desc'))
            
            skill.category = request.POST.get('category')
            skill.targetType = request.POST.get('targetType')
            skill.damage = request.POST.get('damage')
            skill.triggerDamage = request.POST.get('triggerDamage')
            skill.status = request.POST.get('status')
            skill.triggerStatus = request.POST.get('triggerStatus')
            skill.trigger = request.POST.get('trigger')
            skill.property = request.POST.get('property')
            skill.changeFury = int(request.POST.get('changeFury'))
            
            skill.sound = request.POST.get('sound')
            skill.skillres = request.POST.get('skillres')            
            skill.save()
            return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
        else :
            return HttpResponse(json.dumps({'code':0,'message':'cannot find skill!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

    
def getTaskList(request):
    try:
        pageIndex = int(request.GET.get("pageIndex",1))
        startIndex = (pageIndex -1)* PAGE_SIZE
        endIndex = startIndex + PAGE_SIZE
        allrecords = Task.objects.all().order_by('-id')
        pageLength = (allrecords.count() + PAGE_SIZE -1)/ PAGE_SIZE
        tasks = allrecords[startIndex:endIndex]
        tasks = tasks[:]
        for task in tasks:
            lang = langs.getlangById(task.name)
            if lang:
                task.name = lang.value
            lang = langs.getlangById(task.desc)
            if lang:
                task.desc = lang.value
            task.level = task.level
            task.catalog = task.catalog
            task.type = task.type
            task.dests = task.dests
            task.compaignId = task.compaignId
            task.postTaskId = task.postTaskId
            task.startTalk = task.startTalk
            task.endTalk = task.endTalk
            task.deduct = task.deduct
            task.reward = task.reward
                
        _context = Context({
            'tasks':tasks,
            'pageIndex':pageIndex,
            'pageLength':pageLength
        })
    
        _template = loader.get_template('task/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def getTaskJsonList(request):
    try:
        tasks = Task.objects.all().order_by('-id')
        tasks = tasks[:]
        for task in tasks:
            lang = langs.getlangById(task.name)
            if lang:
                task.name = lang.value
            lang = langs.getlangById(task.desc)
            if lang:
                task.desc = lang.value
            task.level = task.level
            task.catalog = task.catalog
            task.type = task.type
            task.dests = task.dests
            task.compaignId = task.compaignId
            task.postTaskId = task.postTaskId
            task.startTalk = task.startTalk
            task.endTalk = task.endTalk
            task.deduct = task.deduct
            task.reward = task.reward
                
        taskJsonList = []
        for task in tasks:
            taskJson = trans.task2Json(task)
            taskJsonList.append(taskJson)
        return HttpResponse(json.dumps({'code':1,'result':taskJsonList}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def searchTask(request):
    try:
        condition = request.GET.get('condition')
        value = request.GET.get('value')
        searchArg = {condition:value}
        tasks = Task.objects.filter(**searchArg).all().order_by('-id')
        for task in tasks:
            lang = langs.getlangById(task.name)
            if lang:
                task.name = lang.value
            lang = langs.getlangById(task.desc)
            if lang:
                task.desc = lang.value
            task.level = task.level
            task.catalog = task.catalog
            task.type = task.type
            task.dests = task.dests
            task.compaignId = task.compaignId
            task.postTaskId = task.postTaskId
            task.startTalk = task.startTalk
            task.endTalk = task.endTalk
            task.deduct = task.deduct
            task.reward = task.reward
                
        _context = Context({
            'tasks':tasks,
            'pageLength':1
        })
    
        _template = loader.get_template('task/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def addTask(request):
    try:
        task = Task()
        task.deployId = 0;
        task.name = request.POST.get('name')         
        task.desc = request.POST.get('desc')         
        task.level = int(request.POST.get('level'))        
        task.catalog = request.POST.get('catalog')         
        task.type = request.POST.get('type')         
        task.dests = request.POST.get('dests')         
        task.compaignId = int(request.POST.get('compaignId'))        
        task.postTaskId = int(request.POST.get('postTaskId'))        
        task.startTalk = request.POST.get('startTalk')         
        task.endTalk = request.POST.get('endTalk')         
        task.deduct = request.POST.get('deduct')         
        task.reward = request.POST.get('reward')                 
        task.save()
        task.deployId = task.id + ModelTypes.Type_Task * 1000000;
        lang = langs.setlang(ModelTypes.Type_Task,task.deployId,'name',task.name)
        task.name = lang.id
        lang = langs.setlang(ModelTypes.Type_Task,task.deployId,'desc',task.desc)
        task.desc = lang.id
        task.save()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')
    

def removeTask(request):
    try:
        taskId = int(request.GET.get('taskId'))
        task = Task.objects.get(id=taskId)
        if task :
            langs.dellangById(task.name)
            langs.dellangById(task.desc)
            task.delete()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def getTask(request):
    try:
        taskId = int(request.GET.get('taskId'))
        task = Task.objects.get(id=taskId)
        lang = langs.getlangById(task.name)
        if lang :
            task.name = lang.value
        lang = langs.getlangById(task.desc)
        if lang :
            task.desc = lang.value
                
        taskJson = trans.task2Json(task)
        
        if task :
            return HttpResponse(json.dumps({'code':1,'result':taskJson}), mimetype='application/json')
        else:
            return HttpResponse(json.dumps({'code':0,'message':'cannot find task!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def editTask(request):
    try:
        taskId = int(request.POST.get('taskId'))
        task = Task.objects.get(id=taskId)
        if task :
            langs.setlangById(task.name,request.POST.get('name'))
            
            langs.setlangById(task.desc,request.POST.get('desc'))
            
            task.level = int(request.POST.get('level'))
            
            task.catalog = request.POST.get('catalog')
            task.type = request.POST.get('type')
            task.dests = request.POST.get('dests')
            task.compaignId = int(request.POST.get('compaignId'))
            
            task.postTaskId = int(request.POST.get('postTaskId'))
            
            task.startTalk = request.POST.get('startTalk')
            task.endTalk = request.POST.get('endTalk')
            task.deduct = request.POST.get('deduct')
            task.reward = request.POST.get('reward')            
            task.save()
            return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
        else :
            return HttpResponse(json.dumps({'code':0,'message':'cannot find task!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

    
def getCardList(request):
    try:
        pageIndex = int(request.GET.get("pageIndex",1))
        startIndex = (pageIndex -1)* PAGE_SIZE
        endIndex = startIndex + PAGE_SIZE
        allrecords = Card.objects.all().order_by('-id')
        pageLength = (allrecords.count() + PAGE_SIZE -1)/ PAGE_SIZE
        cards = allrecords[startIndex:endIndex]
        cards = cards[:]
        for card in cards:
            lang = langs.getlangById(card.name)
            if lang:
                card.name = lang.value
            lang = langs.getlangById(card.desc)
            if lang:
                card.desc = lang.value
            card.module = card.module
            card.catalog = card.catalog
            card.type = card.type
            card.quality = card.quality
            card.award = card.award
            card.condition = card.condition
            card.nextCardId = specials.getCardName(card.nextCardId)
            card.cardres = card.cardres
                
        _context = Context({
            'cards':cards,
            'pageIndex':pageIndex,
            'pageLength':pageLength
        })
    
        _template = loader.get_template('card/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def getCardJsonList(request):
    try:
        cards = Card.objects.all().order_by('-id')
        cards = cards[:]
        for card in cards:
            lang = langs.getlangById(card.name)
            if lang:
                card.name = lang.value
            lang = langs.getlangById(card.desc)
            if lang:
                card.desc = lang.value
            card.module = card.module
            card.catalog = card.catalog
            card.type = card.type
            card.quality = card.quality
            card.award = card.award
            card.condition = card.condition
            card.nextCardId = card.nextCardId
            card.cardres = card.cardres
                
        cardJsonList = []
        for card in cards:
            cardJson = trans.card2Json(card)
            cardJsonList.append(cardJson)
        return HttpResponse(json.dumps({'code':1,'result':cardJsonList}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def searchCard(request):
    try:
        condition = request.GET.get('condition')
        value = request.GET.get('value')
        searchArg = {condition:value}
        cards = Card.objects.filter(**searchArg).all().order_by('-id')
        for card in cards:
            lang = langs.getlangById(card.name)
            if lang:
                card.name = lang.value
            lang = langs.getlangById(card.desc)
            if lang:
                card.desc = lang.value
            card.module = card.module
            card.catalog = card.catalog
            card.type = card.type
            card.quality = card.quality
            card.award = card.award
            card.condition = card.condition
            card.nextCardId = card.nextCardId
            card.cardres = card.cardres
                
        _context = Context({
            'cards':cards,
            'pageLength':1
        })
    
        _template = loader.get_template('card/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def addCard(request):
    try:
        card = Card()
        card.deployId = 0;
        card.name = request.POST.get('name')         
        card.desc = request.POST.get('desc')         
        card.module = request.POST.get('module')         
        card.catalog = int(request.POST.get('catalog'))        
        card.type = int(request.POST.get('type'))        
        card.quality = request.POST.get('quality')         
        card.award = request.POST.get('award')         
        card.condition = request.POST.get('condition')         
        card.nextCardId = request.POST.get('nextCardId')         
        card.cardres = request.POST.get('cardres')                 
        card.save()
        card.deployId = card.id + ModelTypes.Type_Card * 1000000;
        lang = langs.setlang(ModelTypes.Type_Card,card.deployId,'name',card.name)
        card.name = lang.id
        lang = langs.setlang(ModelTypes.Type_Card,card.deployId,'desc',card.desc)
        card.desc = lang.id
        card.save()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')
    

def removeCard(request):
    try:
        cardId = int(request.GET.get('cardId'))
        card = Card.objects.get(id=cardId)
        if card :
            langs.dellangById(card.name)
            langs.dellangById(card.desc)
            card.delete()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def getCard(request):
    try:
        cardId = int(request.GET.get('cardId'))
        card = Card.objects.get(id=cardId)
        lang = langs.getlangById(card.name)
        if lang :
            card.name = lang.value
        lang = langs.getlangById(card.desc)
        if lang :
            card.desc = lang.value
                
        cardJson = trans.card2Json(card)
        
        if card :
            return HttpResponse(json.dumps({'code':1,'result':cardJson}), mimetype='application/json')
        else:
            return HttpResponse(json.dumps({'code':0,'message':'cannot find card!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def editCard(request):
    try:
        cardId = int(request.POST.get('cardId'))
        card = Card.objects.get(id=cardId)
        if card :
            langs.setlangById(card.name,request.POST.get('name'))
            
            langs.setlangById(card.desc,request.POST.get('desc'))
            
            card.module = request.POST.get('module')
            card.catalog = int(request.POST.get('catalog'))
            
            card.type = int(request.POST.get('type'))
            
            card.quality = request.POST.get('quality')
            card.award = request.POST.get('award')
            card.condition = request.POST.get('condition')
            card.nextCardId = request.POST.get('nextCardId')
            card.cardres = request.POST.get('cardres')            
            card.save()
            return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
        else :
            return HttpResponse(json.dumps({'code':0,'message':'cannot find card!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

    
def getMapList(request):
    try:
        pageIndex = int(request.GET.get("pageIndex",1))
        startIndex = (pageIndex -1)* PAGE_SIZE
        endIndex = startIndex + PAGE_SIZE
        allrecords = Map.objects.all().order_by('-id')
        pageLength = (allrecords.count() + PAGE_SIZE -1)/ PAGE_SIZE
        maps = allrecords[startIndex:endIndex]
        maps = maps[:]
        for map in maps:
            lang = langs.getlangById(map.name)
            if lang:
                map.name = lang.value
            map.campaignIds = map.campaignIds
            map.mapres = map.mapres
                
        _context = Context({
            'maps':maps,
            'pageIndex':pageIndex,
            'pageLength':pageLength
        })
    
        _template = loader.get_template('map/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def getMapJsonList(request):
    try:
        maps = Map.objects.all().order_by('-id')
        maps = maps[:]
        for map in maps:
            lang = langs.getlangById(map.name)
            if lang:
                map.name = lang.value
            map.campaignIds = map.campaignIds
            map.mapres = map.mapres
                
        mapJsonList = []
        for map in maps:
            mapJson = trans.map2Json(map)
            mapJsonList.append(mapJson)
        return HttpResponse(json.dumps({'code':1,'result':mapJsonList}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def searchMap(request):
    try:
        condition = request.GET.get('condition')
        value = request.GET.get('value')
        searchArg = {condition:value}
        maps = Map.objects.filter(**searchArg).all().order_by('-id')
        for map in maps:
            lang = langs.getlangById(map.name)
            if lang:
                map.name = lang.value
            map.campaignIds = map.campaignIds
            map.mapres = map.mapres
                
        _context = Context({
            'maps':maps,
            'pageLength':1
        })
    
        _template = loader.get_template('map/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def addMap(request):
    try:
        map = Map()
        map.deployId = 0;
        map.name = request.POST.get('name')         
        map.campaignIds = request.POST.get('campaignIds')         
        map.mapres = request.POST.get('mapres')                 
        map.save()
        map.deployId = map.id + ModelTypes.Type_Map * 1000000;
        lang = langs.setlang(ModelTypes.Type_Map,map.deployId,'name',map.name)
        map.name = lang.id
        map.save()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')
    

def removeMap(request):
    try:
        mapId = int(request.GET.get('mapId'))
        map = Map.objects.get(id=mapId)
        if map :
            langs.dellangById(map.name)
            map.delete()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def getMap(request):
    try:
        mapId = int(request.GET.get('mapId'))
        map = Map.objects.get(id=mapId)
        lang = langs.getlangById(map.name)
        if lang :
            map.name = lang.value
                
        mapJson = trans.map2Json(map)
        
        if map :
            return HttpResponse(json.dumps({'code':1,'result':mapJson}), mimetype='application/json')
        else:
            return HttpResponse(json.dumps({'code':0,'message':'cannot find map!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def editMap(request):
    try:
        mapId = int(request.POST.get('mapId'))
        map = Map.objects.get(id=mapId)
        if map :
            langs.setlangById(map.name,request.POST.get('name'))
            
            map.campaignIds = request.POST.get('campaignIds')
            map.mapres = request.POST.get('mapres')            
            map.save()
            return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
        else :
            return HttpResponse(json.dumps({'code':0,'message':'cannot find map!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

    
def getCampaignList(request):
    try:
        pageIndex = int(request.GET.get("pageIndex",1))
        startIndex = (pageIndex -1)* PAGE_SIZE
        endIndex = startIndex + PAGE_SIZE
        allrecords = Campaign.objects.all().order_by('-id')
        pageLength = (allrecords.count() + PAGE_SIZE -1)/ PAGE_SIZE
        campaigns = allrecords[startIndex:endIndex]
        campaigns = campaigns[:]
        for campaign in campaigns:
            lang = langs.getlangById(campaign.name)
            if lang:
                campaign.name = lang.value
            lang = langs.getlangById(campaign.desc)
            if lang:
                campaign.desc = lang.value
            campaign.battlePointIds = campaign.battlePointIds
            campaign.status = campaign.status
            campaign.vip = campaign.vip
            campaign.mapId = specials.getMapName(campaign.mapId)
            campaign.userLevel = campaign.userLevel
            campaign.coorX = campaign.coorX
            campaign.coorY = campaign.coorY
            campaign.music = campaign.music
            campaign.frontCompaignIds = campaign.frontCompaignIds
            campaign.afterCompaignIds = campaign.afterCompaignIds
                
        _context = Context({
            'campaigns':campaigns,
            'pageIndex':pageIndex,
            'pageLength':pageLength
        })
    
        _template = loader.get_template('campaign/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def getCampaignJsonList(request):
    try:
        campaigns = Campaign.objects.all().order_by('-id')
        campaigns = campaigns[:]
        for campaign in campaigns:
            lang = langs.getlangById(campaign.name)
            if lang:
                campaign.name = lang.value
            lang = langs.getlangById(campaign.desc)
            if lang:
                campaign.desc = lang.value
            campaign.battlePointIds = campaign.battlePointIds
            campaign.status = campaign.status
            campaign.vip = campaign.vip
            campaign.mapId = campaign.mapId
            campaign.userLevel = campaign.userLevel
            campaign.coorX = campaign.coorX
            campaign.coorY = campaign.coorY
            campaign.music = campaign.music
            campaign.frontCompaignIds = campaign.frontCompaignIds
            campaign.afterCompaignIds = campaign.afterCompaignIds
                
        campaignJsonList = []
        for campaign in campaigns:
            campaignJson = trans.campaign2Json(campaign)
            campaignJsonList.append(campaignJson)
        return HttpResponse(json.dumps({'code':1,'result':campaignJsonList}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def searchCampaign(request):
    try:
        condition = request.GET.get('condition')
        value = request.GET.get('value')
        searchArg = {condition:value}
        campaigns = Campaign.objects.filter(**searchArg).all().order_by('-id')
        for campaign in campaigns:
            lang = langs.getlangById(campaign.name)
            if lang:
                campaign.name = lang.value
            lang = langs.getlangById(campaign.desc)
            if lang:
                campaign.desc = lang.value
            campaign.battlePointIds = campaign.battlePointIds
            campaign.status = campaign.status
            campaign.vip = campaign.vip
            campaign.mapId = campaign.mapId
            campaign.userLevel = campaign.userLevel
            campaign.coorX = campaign.coorX
            campaign.coorY = campaign.coorY
            campaign.music = campaign.music
            campaign.frontCompaignIds = campaign.frontCompaignIds
            campaign.afterCompaignIds = campaign.afterCompaignIds
                
        _context = Context({
            'campaigns':campaigns,
            'pageLength':1
        })
    
        _template = loader.get_template('campaign/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def addCampaign(request):
    try:
        campaign = Campaign()
        campaign.deployId = 0;
        campaign.name = request.POST.get('name')         
        campaign.desc = request.POST.get('desc')         
        campaign.battlePointIds = request.POST.get('battlePointIds')         
        campaign.status = request.POST.get('status')         
        campaign.vip = request.POST.get('vip')         
        campaign.mapId = request.POST.get('mapId')         
        campaign.userLevel = int(request.POST.get('userLevel'))        
        campaign.coorX = int(request.POST.get('coorX'))        
        campaign.coorY = int(request.POST.get('coorY'))        
        campaign.music = request.POST.get('music')         
        campaign.frontCompaignIds = request.POST.get('frontCompaignIds')         
        campaign.afterCompaignIds = request.POST.get('afterCompaignIds')                 
        campaign.save()
        campaign.deployId = campaign.id + ModelTypes.Type_Campaign * 1000000;
        lang = langs.setlang(ModelTypes.Type_Campaign,campaign.deployId,'name',campaign.name)
        campaign.name = lang.id
        lang = langs.setlang(ModelTypes.Type_Campaign,campaign.deployId,'desc',campaign.desc)
        campaign.desc = lang.id
        campaign.save()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')
    

def removeCampaign(request):
    try:
        campaignId = int(request.GET.get('campaignId'))
        campaign = Campaign.objects.get(id=campaignId)
        if campaign :
            langs.dellangById(campaign.name)
            langs.dellangById(campaign.desc)
            campaign.delete()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def getCampaign(request):
    try:
        campaignId = int(request.GET.get('campaignId'))
        campaign = Campaign.objects.get(id=campaignId)
        lang = langs.getlangById(campaign.name)
        if lang :
            campaign.name = lang.value
        lang = langs.getlangById(campaign.desc)
        if lang :
            campaign.desc = lang.value
                
        campaignJson = trans.campaign2Json(campaign)
        
        if campaign :
            return HttpResponse(json.dumps({'code':1,'result':campaignJson}), mimetype='application/json')
        else:
            return HttpResponse(json.dumps({'code':0,'message':'cannot find campaign!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def editCampaign(request):
    try:
        campaignId = int(request.POST.get('campaignId'))
        campaign = Campaign.objects.get(id=campaignId)
        if campaign :
            langs.setlangById(campaign.name,request.POST.get('name'))
            
            langs.setlangById(campaign.desc,request.POST.get('desc'))
            
            campaign.battlePointIds = request.POST.get('battlePointIds')
            campaign.status = request.POST.get('status')
            campaign.vip = request.POST.get('vip')
            campaign.mapId = request.POST.get('mapId')
            campaign.userLevel = int(request.POST.get('userLevel'))
            
            campaign.coorX = int(request.POST.get('coorX'))
            
            campaign.coorY = int(request.POST.get('coorY'))
            
            campaign.music = request.POST.get('music')
            campaign.frontCompaignIds = request.POST.get('frontCompaignIds')
            campaign.afterCompaignIds = request.POST.get('afterCompaignIds')            
            campaign.save()
            return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
        else :
            return HttpResponse(json.dumps({'code':0,'message':'cannot find campaign!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

    
def getBattle_pointList(request):
    try:
        pageIndex = int(request.GET.get("pageIndex",1))
        startIndex = (pageIndex -1)* PAGE_SIZE
        endIndex = startIndex + PAGE_SIZE
        allrecords = Battle_point.objects.all().order_by('-id')
        pageLength = (allrecords.count() + PAGE_SIZE -1)/ PAGE_SIZE
        battle_points = allrecords[startIndex:endIndex]
        battle_points = battle_points[:]
        for battle_point in battle_points:
            lang = langs.getlangById(battle_point.name)
            if lang:
                battle_point.name = lang.value
            lang = langs.getlangById(battle_point.desc)
            if lang:
                battle_point.desc = lang.value
            battle_point.npcId = specials.getNPCName(battle_point.npcId)
            battle_point.campaignId = specials.getCampaignName(battle_point.campaignId)
            battle_point.weatherIds = battle_point.weatherIds
            battle_point.roundMax = battle_point.roundMax
            battle_point.leaderDead = battle_point.leaderDead
            battle_point.roundLimit = battle_point.roundLimit
            battle_point.leader = battle_point.leader
            battle_point.round = battle_point.round
            battle_point.endCondition = battle_point.endCondition
            battle_point.attackerMax = battle_point.attackerMax
            battle_point.defenderMax = battle_point.defenderMax
            battle_point.areaId = battle_point.areaId
            battle_point.drops = battle_point.drops
            battle_point.energyCost = battle_point.energyCost
            battle_point.passCount = battle_point.passCount
            battle_point.difficulty = battle_point.difficulty
            battle_point.starMax = battle_point.starMax
            battle_point.frontBattlePointIds = battle_point.frontBattlePointIds
            battle_point.afterBattlePointIds = battle_point.afterBattlePointIds
                
        _context = Context({
            'battle_points':battle_points,
            'pageIndex':pageIndex,
            'pageLength':pageLength
        })
    
        _template = loader.get_template('battle_point/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def getBattle_pointJsonList(request):
    try:
        battle_points = Battle_point.objects.all().order_by('-id')
        battle_points = battle_points[:]
        for battle_point in battle_points:
            lang = langs.getlangById(battle_point.name)
            if lang:
                battle_point.name = lang.value
            lang = langs.getlangById(battle_point.desc)
            if lang:
                battle_point.desc = lang.value
            battle_point.npcId = battle_point.npcId
            battle_point.campaignId = battle_point.campaignId
            battle_point.weatherIds = battle_point.weatherIds
            battle_point.roundMax = battle_point.roundMax
            battle_point.leaderDead = battle_point.leaderDead
            battle_point.roundLimit = battle_point.roundLimit
            battle_point.leader = battle_point.leader
            battle_point.round = battle_point.round
            battle_point.endCondition = battle_point.endCondition
            battle_point.attackerMax = battle_point.attackerMax
            battle_point.defenderMax = battle_point.defenderMax
            battle_point.areaId = battle_point.areaId
            battle_point.drops = battle_point.drops
            battle_point.energyCost = battle_point.energyCost
            battle_point.passCount = battle_point.passCount
            battle_point.difficulty = battle_point.difficulty
            battle_point.starMax = battle_point.starMax
            battle_point.frontBattlePointIds = battle_point.frontBattlePointIds
            battle_point.afterBattlePointIds = battle_point.afterBattlePointIds
                
        battle_pointJsonList = []
        for battle_point in battle_points:
            battle_pointJson = trans.battle_point2Json(battle_point)
            battle_pointJsonList.append(battle_pointJson)
        return HttpResponse(json.dumps({'code':1,'result':battle_pointJsonList}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def searchBattle_point(request):
    try:
        condition = request.GET.get('condition')
        value = request.GET.get('value')
        searchArg = {condition:value}
        battle_points = Battle_point.objects.filter(**searchArg).all().order_by('-id')
        for battle_point in battle_points:
            lang = langs.getlangById(battle_point.name)
            if lang:
                battle_point.name = lang.value
            lang = langs.getlangById(battle_point.desc)
            if lang:
                battle_point.desc = lang.value
            battle_point.npcId = battle_point.npcId
            battle_point.campaignId = battle_point.campaignId
            battle_point.weatherIds = battle_point.weatherIds
            battle_point.roundMax = battle_point.roundMax
            battle_point.leaderDead = battle_point.leaderDead
            battle_point.roundLimit = battle_point.roundLimit
            battle_point.leader = battle_point.leader
            battle_point.round = battle_point.round
            battle_point.endCondition = battle_point.endCondition
            battle_point.attackerMax = battle_point.attackerMax
            battle_point.defenderMax = battle_point.defenderMax
            battle_point.areaId = battle_point.areaId
            battle_point.drops = battle_point.drops
            battle_point.energyCost = battle_point.energyCost
            battle_point.passCount = battle_point.passCount
            battle_point.difficulty = battle_point.difficulty
            battle_point.starMax = battle_point.starMax
            battle_point.frontBattlePointIds = battle_point.frontBattlePointIds
            battle_point.afterBattlePointIds = battle_point.afterBattlePointIds
                
        _context = Context({
            'battle_points':battle_points,
            'pageLength':1
        })
    
        _template = loader.get_template('battle_point/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def addBattle_point(request):
    try:
        battle_point = Battle_point()
        battle_point.deployId = 0;
        battle_point.name = request.POST.get('name')         
        battle_point.desc = request.POST.get('desc')         
        battle_point.npcId = request.POST.get('npcId')         
        battle_point.campaignId = request.POST.get('campaignId')         
        battle_point.weatherIds = request.POST.get('weatherIds')         
        battle_point.roundMax = int(request.POST.get('roundMax'))        
        battle_point.leaderDead = request.POST.get('leaderDead')         
        battle_point.roundLimit = request.POST.get('roundLimit')         
        battle_point.leader = request.POST.get('leader')         
        battle_point.round = request.POST.get('round')         
        battle_point.endCondition = request.POST.get('endCondition')         
        battle_point.attackerMax = int(request.POST.get('attackerMax'))        
        battle_point.defenderMax = int(request.POST.get('defenderMax'))        
        battle_point.areaId = int(request.POST.get('areaId'))        
        battle_point.drops = request.POST.get('drops')         
        battle_point.energyCost = int(request.POST.get('energyCost'))        
        battle_point.passCount = int(request.POST.get('passCount'))        
        battle_point.difficulty = int(request.POST.get('difficulty'))        
        battle_point.starMax = int(request.POST.get('starMax'))        
        battle_point.frontBattlePointIds = request.POST.get('frontBattlePointIds')         
        battle_point.afterBattlePointIds = request.POST.get('afterBattlePointIds')                 
        battle_point.save()
        battle_point.deployId = battle_point.id + ModelTypes.Type_Battle_point * 1000000;
        lang = langs.setlang(ModelTypes.Type_Battle_point,battle_point.deployId,'name',battle_point.name)
        battle_point.name = lang.id
        lang = langs.setlang(ModelTypes.Type_Battle_point,battle_point.deployId,'desc',battle_point.desc)
        battle_point.desc = lang.id
        battle_point.save()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')
    

def removeBattle_point(request):
    try:
        battle_pointId = int(request.GET.get('battle_pointId'))
        battle_point = Battle_point.objects.get(id=battle_pointId)
        if battle_point :
            langs.dellangById(battle_point.name)
            langs.dellangById(battle_point.desc)
            battle_point.delete()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def getBattle_point(request):
    try:
        battle_pointId = int(request.GET.get('battle_pointId'))
        battle_point = Battle_point.objects.get(id=battle_pointId)
        lang = langs.getlangById(battle_point.name)
        if lang :
            battle_point.name = lang.value
        lang = langs.getlangById(battle_point.desc)
        if lang :
            battle_point.desc = lang.value
                
        battle_pointJson = trans.battle_point2Json(battle_point)
        
        if battle_point :
            return HttpResponse(json.dumps({'code':1,'result':battle_pointJson}), mimetype='application/json')
        else:
            return HttpResponse(json.dumps({'code':0,'message':'cannot find battle_point!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def editBattle_point(request):
    try:
        battle_pointId = int(request.POST.get('battle_pointId'))
        battle_point = Battle_point.objects.get(id=battle_pointId)
        if battle_point :
            langs.setlangById(battle_point.name,request.POST.get('name'))
            
            langs.setlangById(battle_point.desc,request.POST.get('desc'))
            
            battle_point.npcId = request.POST.get('npcId')
            battle_point.campaignId = request.POST.get('campaignId')
            battle_point.weatherIds = request.POST.get('weatherIds')
            battle_point.roundMax = int(request.POST.get('roundMax'))
            
            battle_point.leaderDead = request.POST.get('leaderDead')
            battle_point.roundLimit = request.POST.get('roundLimit')
            battle_point.leader = request.POST.get('leader')
            battle_point.round = request.POST.get('round')
            battle_point.endCondition = request.POST.get('endCondition')
            battle_point.attackerMax = int(request.POST.get('attackerMax'))
            
            battle_point.defenderMax = int(request.POST.get('defenderMax'))
            
            battle_point.areaId = int(request.POST.get('areaId'))
            
            battle_point.drops = request.POST.get('drops')
            battle_point.energyCost = int(request.POST.get('energyCost'))
            
            battle_point.passCount = int(request.POST.get('passCount'))
            
            battle_point.difficulty = int(request.POST.get('difficulty'))
            
            battle_point.starMax = int(request.POST.get('starMax'))
            
            battle_point.frontBattlePointIds = request.POST.get('frontBattlePointIds')
            battle_point.afterBattlePointIds = request.POST.get('afterBattlePointIds')            
            battle_point.save()
            return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
        else :
            return HttpResponse(json.dumps({'code':0,'message':'cannot find battle_point!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

    
def getLevel_propList(request):
    try:
        pageIndex = int(request.GET.get("pageIndex",1))
        startIndex = (pageIndex -1)* PAGE_SIZE
        endIndex = startIndex + PAGE_SIZE
        allrecords = Level_prop.objects.all().order_by('-id')
        pageLength = (allrecords.count() + PAGE_SIZE -1)/ PAGE_SIZE
        level_props = allrecords[startIndex:endIndex]
        level_props = level_props[:]
        for level_prop in level_props:
            level_prop.exp = level_prop.exp
            level_prop.energyMax = level_prop.energyMax
            level_prop.battleCount = level_prop.battleCount
            level_prop.employeeCount = level_prop.employeeCount
            level_prop.skillCount = level_prop.skillCount
            level_prop.hp = level_prop.hp
            level_prop.physicalatk = level_prop.physicalatk
            level_prop.physicaldef = level_prop.physicaldef
            level_prop.magicatk = level_prop.magicatk
            level_prop.magicdef = level_prop.magicdef
            level_prop.award = level_prop.award
            level_prop.logos = level_prop.logos
                
        _context = Context({
            'level_props':level_props,
            'pageIndex':pageIndex,
            'pageLength':pageLength
        })
    
        _template = loader.get_template('level_prop/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def getLevel_propJsonList(request):
    try:
        level_props = Level_prop.objects.all().order_by('-id')
        level_props = level_props[:]
        for level_prop in level_props:
            level_prop.exp = level_prop.exp
            level_prop.energyMax = level_prop.energyMax
            level_prop.battleCount = level_prop.battleCount
            level_prop.employeeCount = level_prop.employeeCount
            level_prop.skillCount = level_prop.skillCount
            level_prop.hp = level_prop.hp
            level_prop.physicalatk = level_prop.physicalatk
            level_prop.physicaldef = level_prop.physicaldef
            level_prop.magicatk = level_prop.magicatk
            level_prop.magicdef = level_prop.magicdef
            level_prop.award = level_prop.award
            level_prop.logos = level_prop.logos
                
        level_propJsonList = []
        for level_prop in level_props:
            level_propJson = trans.level_prop2Json(level_prop)
            level_propJsonList.append(level_propJson)
        return HttpResponse(json.dumps({'code':1,'result':level_propJsonList}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def searchLevel_prop(request):
    try:
        condition = request.GET.get('condition')
        value = request.GET.get('value')
        searchArg = {condition:value}
        level_props = Level_prop.objects.filter(**searchArg).all().order_by('-id')
        for level_prop in level_props:
            level_prop.exp = level_prop.exp
            level_prop.energyMax = level_prop.energyMax
            level_prop.battleCount = level_prop.battleCount
            level_prop.employeeCount = level_prop.employeeCount
            level_prop.skillCount = level_prop.skillCount
            level_prop.hp = level_prop.hp
            level_prop.physicalatk = level_prop.physicalatk
            level_prop.physicaldef = level_prop.physicaldef
            level_prop.magicatk = level_prop.magicatk
            level_prop.magicdef = level_prop.magicdef
            level_prop.award = level_prop.award
            level_prop.logos = level_prop.logos
                
        _context = Context({
            'level_props':level_props,
            'pageLength':1
        })
    
        _template = loader.get_template('level_prop/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def addLevel_prop(request):
    try:
        level_prop = Level_prop()
        level_prop.deployId = 0;
        level_prop.exp = int(request.POST.get('exp'))        
        level_prop.energyMax = int(request.POST.get('energyMax'))        
        level_prop.battleCount = int(request.POST.get('battleCount'))        
        level_prop.employeeCount = int(request.POST.get('employeeCount'))        
        level_prop.skillCount = int(request.POST.get('skillCount'))        
        level_prop.hp = int(request.POST.get('hp'))        
        level_prop.physicalatk = int(request.POST.get('physicalatk'))        
        level_prop.physicaldef = int(request.POST.get('physicaldef'))        
        level_prop.magicatk = int(request.POST.get('magicatk'))        
        level_prop.magicdef = int(request.POST.get('magicdef'))        
        level_prop.award = request.POST.get('award')         
        level_prop.logos = request.POST.get('logos')                 
        level_prop.save()
        level_prop.deployId = level_prop.id + ModelTypes.Type_Level_prop * 1000000;
        level_prop.save()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')
    

def removeLevel_prop(request):
    try:
        level_propId = int(request.GET.get('level_propId'))
        level_prop = Level_prop.objects.get(id=level_propId)
        if level_prop :
            level_prop.delete()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def getLevel_prop(request):
    try:
        level_propId = int(request.GET.get('level_propId'))
        level_prop = Level_prop.objects.get(id=level_propId)
                
        level_propJson = trans.level_prop2Json(level_prop)
        
        if level_prop :
            return HttpResponse(json.dumps({'code':1,'result':level_propJson}), mimetype='application/json')
        else:
            return HttpResponse(json.dumps({'code':0,'message':'cannot find level_prop!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def editLevel_prop(request):
    try:
        level_propId = int(request.POST.get('level_propId'))
        level_prop = Level_prop.objects.get(id=level_propId)
        if level_prop :
            level_prop.exp = int(request.POST.get('exp'))
            
            level_prop.energyMax = int(request.POST.get('energyMax'))
            
            level_prop.battleCount = int(request.POST.get('battleCount'))
            
            level_prop.employeeCount = int(request.POST.get('employeeCount'))
            
            level_prop.skillCount = int(request.POST.get('skillCount'))
            
            level_prop.hp = int(request.POST.get('hp'))
            
            level_prop.physicalatk = int(request.POST.get('physicalatk'))
            
            level_prop.physicaldef = int(request.POST.get('physicaldef'))
            
            level_prop.magicatk = int(request.POST.get('magicatk'))
            
            level_prop.magicdef = int(request.POST.get('magicdef'))
            
            level_prop.award = request.POST.get('award')
            level_prop.logos = request.POST.get('logos')            
            level_prop.save()
            return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
        else :
            return HttpResponse(json.dumps({'code':0,'message':'cannot find level_prop!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

    
def getNpcList(request):
    try:
        pageIndex = int(request.GET.get("pageIndex",1))
        startIndex = (pageIndex -1)* PAGE_SIZE
        endIndex = startIndex + PAGE_SIZE
        allrecords = Npc.objects.all().order_by('-id')
        pageLength = (allrecords.count() + PAGE_SIZE -1)/ PAGE_SIZE
        npcs = allrecords[startIndex:endIndex]
        npcs = npcs[:]
        for npc in npcs:
            lang = langs.getlangById(npc.name)
            if lang:
                npc.name = lang.value
            lang = langs.getlangById(npc.desc)
            if lang:
                npc.desc = lang.value
            npc.npcUnitIds = npc.npcUnitIds
            npc.npcres = npc.npcres
                
        _context = Context({
            'npcs':npcs,
            'pageIndex':pageIndex,
            'pageLength':pageLength
        })
    
        _template = loader.get_template('npc/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def getNpcJsonList(request):
    try:
        npcs = Npc.objects.all().order_by('-id')
        npcs = npcs[:]
        for npc in npcs:
            lang = langs.getlangById(npc.name)
            if lang:
                npc.name = lang.value
            lang = langs.getlangById(npc.desc)
            if lang:
                npc.desc = lang.value
            npc.npcUnitIds = npc.npcUnitIds
            npc.npcres = npc.npcres
                
        npcJsonList = []
        for npc in npcs:
            npcJson = trans.npc2Json(npc)
            npcJsonList.append(npcJson)
        return HttpResponse(json.dumps({'code':1,'result':npcJsonList}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def searchNpc(request):
    try:
        condition = request.GET.get('condition')
        value = request.GET.get('value')
        searchArg = {condition:value}
        npcs = Npc.objects.filter(**searchArg).all().order_by('-id')
        for npc in npcs:
            lang = langs.getlangById(npc.name)
            if lang:
                npc.name = lang.value
            lang = langs.getlangById(npc.desc)
            if lang:
                npc.desc = lang.value
            npc.npcUnitIds = npc.npcUnitIds
            npc.npcres = npc.npcres
                
        _context = Context({
            'npcs':npcs,
            'pageLength':1
        })
    
        _template = loader.get_template('npc/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def addNpc(request):
    try:
        npc = Npc()
        npc.deployId = 0;
        npc.name = request.POST.get('name')         
        npc.desc = request.POST.get('desc')         
        npc.npcUnitIds = request.POST.get('npcUnitIds')         
        npc.npcres = request.POST.get('npcres')                 
        npc.save()
        npc.deployId = npc.id + ModelTypes.Type_Npc * 1000000;
        lang = langs.setlang(ModelTypes.Type_Npc,npc.deployId,'name',npc.name)
        npc.name = lang.id
        lang = langs.setlang(ModelTypes.Type_Npc,npc.deployId,'desc',npc.desc)
        npc.desc = lang.id
        npc.save()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')
    

def removeNpc(request):
    try:
        npcId = int(request.GET.get('npcId'))
        npc = Npc.objects.get(id=npcId)
        if npc :
            langs.dellangById(npc.name)
            langs.dellangById(npc.desc)
            npc.delete()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def getNpc(request):
    try:
        npcId = int(request.GET.get('npcId'))
        npc = Npc.objects.get(id=npcId)
        lang = langs.getlangById(npc.name)
        if lang :
            npc.name = lang.value
        lang = langs.getlangById(npc.desc)
        if lang :
            npc.desc = lang.value
                
        npcJson = trans.npc2Json(npc)
        
        if npc :
            return HttpResponse(json.dumps({'code':1,'result':npcJson}), mimetype='application/json')
        else:
            return HttpResponse(json.dumps({'code':0,'message':'cannot find npc!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def editNpc(request):
    try:
        npcId = int(request.POST.get('npcId'))
        npc = Npc.objects.get(id=npcId)
        if npc :
            langs.setlangById(npc.name,request.POST.get('name'))
            
            langs.setlangById(npc.desc,request.POST.get('desc'))
            
            npc.npcUnitIds = request.POST.get('npcUnitIds')
            npc.npcres = request.POST.get('npcres')            
            npc.save()
            return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
        else :
            return HttpResponse(json.dumps({'code':0,'message':'cannot find npc!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

    
def getNpc_unitList(request):
    try:
        pageIndex = int(request.GET.get("pageIndex",1))
        startIndex = (pageIndex -1)* PAGE_SIZE
        endIndex = startIndex + PAGE_SIZE
        allrecords = Npc_unit.objects.all().order_by('-id')
        pageLength = (allrecords.count() + PAGE_SIZE -1)/ PAGE_SIZE
        npc_units = allrecords[startIndex:endIndex]
        npc_units = npc_units[:]
        for npc_unit in npc_units:
            lang = langs.getlangById(npc_unit.name)
            if lang:
                npc_unit.name = lang.value
            lang = langs.getlangById(npc_unit.desc)
            if lang:
                npc_unit.desc = lang.value
            npc_unit.level = npc_unit.level
            npc_unit.career = npc_unit.career
            npc_unit.gender = npc_unit.gender
            npc_unit.npcId = specials.getNPCName(npc_unit.npcId)
            npc_unit.idx = npc_unit.idx
            npc_unit.pos = npc_unit.pos
            npc_unit.hp = npc_unit.hp
            npc_unit.attackId = npc_unit.attackId
            npc_unit.physicalatk = npc_unit.physicalatk
            npc_unit.physicaldef = npc_unit.physicaldef
            npc_unit.magicatk = npc_unit.magicatk
            npc_unit.magicdef = npc_unit.magicdef
            npc_unit.furyAttackRate = npc_unit.furyAttackRate
            npc_unit.hardAttackRate = npc_unit.hardAttackRate
            npc_unit.hurtIncValue = npc_unit.hurtIncValue
            npc_unit.hurtDecValue = npc_unit.hurtDecValue
            npc_unit.hurtIncPercent = npc_unit.hurtIncPercent
            npc_unit.hurtDecPercent = npc_unit.hurtDecPercent
            npc_unit.skills = npc_unit.skills
            npc_unit.equipment = npc_unit.equipment
            npc_unit.style = npc_unit.style
            npc_unit.npcunitres = npc_unit.npcunitres
                
        _context = Context({
            'npc_units':npc_units,
            'pageIndex':pageIndex,
            'pageLength':pageLength
        })
    
        _template = loader.get_template('npc_unit/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def getNpc_unitJsonList(request):
    try:
        npc_units = Npc_unit.objects.all().order_by('-id')
        npc_units = npc_units[:]
        for npc_unit in npc_units:
            lang = langs.getlangById(npc_unit.name)
            if lang:
                npc_unit.name = lang.value
            lang = langs.getlangById(npc_unit.desc)
            if lang:
                npc_unit.desc = lang.value
            npc_unit.level = npc_unit.level
            npc_unit.career = npc_unit.career
            npc_unit.gender = npc_unit.gender
            npc_unit.npcId = npc_unit.npcId
            npc_unit.idx = npc_unit.idx
            npc_unit.pos = npc_unit.pos
            npc_unit.hp = npc_unit.hp
            npc_unit.attackId = npc_unit.attackId
            npc_unit.physicalatk = npc_unit.physicalatk
            npc_unit.physicaldef = npc_unit.physicaldef
            npc_unit.magicatk = npc_unit.magicatk
            npc_unit.magicdef = npc_unit.magicdef
            npc_unit.furyAttackRate = npc_unit.furyAttackRate
            npc_unit.hardAttackRate = npc_unit.hardAttackRate
            npc_unit.hurtIncValue = npc_unit.hurtIncValue
            npc_unit.hurtDecValue = npc_unit.hurtDecValue
            npc_unit.hurtIncPercent = npc_unit.hurtIncPercent
            npc_unit.hurtDecPercent = npc_unit.hurtDecPercent
            npc_unit.skills = npc_unit.skills
            npc_unit.equipment = npc_unit.equipment
            npc_unit.style = npc_unit.style
            npc_unit.npcunitres = npc_unit.npcunitres
                
        npc_unitJsonList = []
        for npc_unit in npc_units:
            npc_unitJson = trans.npc_unit2Json(npc_unit)
            npc_unitJsonList.append(npc_unitJson)
        return HttpResponse(json.dumps({'code':1,'result':npc_unitJsonList}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def searchNpc_unit(request):
    try:
        condition = request.GET.get('condition')
        value = request.GET.get('value')
        searchArg = {condition:value}
        npc_units = Npc_unit.objects.filter(**searchArg).all().order_by('-id')
        for npc_unit in npc_units:
            lang = langs.getlangById(npc_unit.name)
            if lang:
                npc_unit.name = lang.value
            lang = langs.getlangById(npc_unit.desc)
            if lang:
                npc_unit.desc = lang.value
            npc_unit.level = npc_unit.level
            npc_unit.career = npc_unit.career
            npc_unit.gender = npc_unit.gender
            npc_unit.npcId = npc_unit.npcId
            npc_unit.idx = npc_unit.idx
            npc_unit.pos = npc_unit.pos
            npc_unit.hp = npc_unit.hp
            npc_unit.attackId = npc_unit.attackId
            npc_unit.physicalatk = npc_unit.physicalatk
            npc_unit.physicaldef = npc_unit.physicaldef
            npc_unit.magicatk = npc_unit.magicatk
            npc_unit.magicdef = npc_unit.magicdef
            npc_unit.furyAttackRate = npc_unit.furyAttackRate
            npc_unit.hardAttackRate = npc_unit.hardAttackRate
            npc_unit.hurtIncValue = npc_unit.hurtIncValue
            npc_unit.hurtDecValue = npc_unit.hurtDecValue
            npc_unit.hurtIncPercent = npc_unit.hurtIncPercent
            npc_unit.hurtDecPercent = npc_unit.hurtDecPercent
            npc_unit.skills = npc_unit.skills
            npc_unit.equipment = npc_unit.equipment
            npc_unit.style = npc_unit.style
            npc_unit.npcunitres = npc_unit.npcunitres
                
        _context = Context({
            'npc_units':npc_units,
            'pageLength':1
        })
    
        _template = loader.get_template('npc_unit/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def addNpc_unit(request):
    try:
        npc_unit = Npc_unit()
        npc_unit.deployId = 0;
        npc_unit.name = request.POST.get('name')         
        npc_unit.desc = request.POST.get('desc')         
        npc_unit.level = int(request.POST.get('level'))        
        npc_unit.career = request.POST.get('career')         
        npc_unit.gender = request.POST.get('gender')         
        npc_unit.npcId = request.POST.get('npcId')         
        npc_unit.idx = int(request.POST.get('idx'))        
        npc_unit.pos = int(request.POST.get('pos'))        
        npc_unit.hp = int(request.POST.get('hp'))        
        npc_unit.attackId = int(request.POST.get('attackId'))        
        npc_unit.physicalatk = int(request.POST.get('physicalatk'))        
        npc_unit.physicaldef = int(request.POST.get('physicaldef'))        
        npc_unit.magicatk = int(request.POST.get('magicatk'))        
        npc_unit.magicdef = int(request.POST.get('magicdef'))        
        npc_unit.furyAttackRate = int(request.POST.get('furyAttackRate'))        
        npc_unit.hardAttackRate = int(request.POST.get('hardAttackRate'))        
        npc_unit.hurtIncValue = int(request.POST.get('hurtIncValue'))        
        npc_unit.hurtDecValue = int(request.POST.get('hurtDecValue'))        
        npc_unit.hurtIncPercent = int(request.POST.get('hurtIncPercent'))        
        npc_unit.hurtDecPercent = int(request.POST.get('hurtDecPercent'))        
        npc_unit.skills = request.POST.get('skills')         
        npc_unit.equipment = request.POST.get('equipment')         
        npc_unit.style = request.POST.get('style')         
        npc_unit.npcunitres = request.POST.get('npcunitres')                 
        npc_unit.save()
        npc_unit.deployId = npc_unit.id + ModelTypes.Type_Npc_unit * 1000000;
        lang = langs.setlang(ModelTypes.Type_Npc_unit,npc_unit.deployId,'name',npc_unit.name)
        npc_unit.name = lang.id
        lang = langs.setlang(ModelTypes.Type_Npc_unit,npc_unit.deployId,'desc',npc_unit.desc)
        npc_unit.desc = lang.id
        npc_unit.save()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')
    

def removeNpc_unit(request):
    try:
        npc_unitId = int(request.GET.get('npc_unitId'))
        npc_unit = Npc_unit.objects.get(id=npc_unitId)
        if npc_unit :
            langs.dellangById(npc_unit.name)
            langs.dellangById(npc_unit.desc)
            npc_unit.delete()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def getNpc_unit(request):
    try:
        npc_unitId = int(request.GET.get('npc_unitId'))
        npc_unit = Npc_unit.objects.get(id=npc_unitId)
        lang = langs.getlangById(npc_unit.name)
        if lang :
            npc_unit.name = lang.value
        lang = langs.getlangById(npc_unit.desc)
        if lang :
            npc_unit.desc = lang.value
                
        npc_unitJson = trans.npc_unit2Json(npc_unit)
        
        if npc_unit :
            return HttpResponse(json.dumps({'code':1,'result':npc_unitJson}), mimetype='application/json')
        else:
            return HttpResponse(json.dumps({'code':0,'message':'cannot find npc_unit!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def editNpc_unit(request):
    try:
        npc_unitId = int(request.POST.get('npc_unitId'))
        npc_unit = Npc_unit.objects.get(id=npc_unitId)
        if npc_unit :
            langs.setlangById(npc_unit.name,request.POST.get('name'))
            
            langs.setlangById(npc_unit.desc,request.POST.get('desc'))
            
            npc_unit.level = int(request.POST.get('level'))
            
            npc_unit.career = request.POST.get('career')
            npc_unit.gender = request.POST.get('gender')
            npc_unit.npcId = request.POST.get('npcId')
            npc_unit.idx = int(request.POST.get('idx'))
            
            npc_unit.pos = int(request.POST.get('pos'))
            
            npc_unit.hp = int(request.POST.get('hp'))
            
            npc_unit.attackId = int(request.POST.get('attackId'))
            
            npc_unit.physicalatk = int(request.POST.get('physicalatk'))
            
            npc_unit.physicaldef = int(request.POST.get('physicaldef'))
            
            npc_unit.magicatk = int(request.POST.get('magicatk'))
            
            npc_unit.magicdef = int(request.POST.get('magicdef'))
            
            npc_unit.furyAttackRate = int(request.POST.get('furyAttackRate'))
            
            npc_unit.hardAttackRate = int(request.POST.get('hardAttackRate'))
            
            npc_unit.hurtIncValue = int(request.POST.get('hurtIncValue'))
            
            npc_unit.hurtDecValue = int(request.POST.get('hurtDecValue'))
            
            npc_unit.hurtIncPercent = int(request.POST.get('hurtIncPercent'))
            
            npc_unit.hurtDecPercent = int(request.POST.get('hurtDecPercent'))
            
            npc_unit.skills = request.POST.get('skills')
            npc_unit.equipment = request.POST.get('equipment')
            npc_unit.style = request.POST.get('style')
            npc_unit.npcunitres = request.POST.get('npcunitres')            
            npc_unit.save()
            return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
        else :
            return HttpResponse(json.dumps({'code':0,'message':'cannot find npc_unit!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

    
def getShop_itemList(request):
    try:
        pageIndex = int(request.GET.get("pageIndex",1))
        startIndex = (pageIndex -1)* PAGE_SIZE
        endIndex = startIndex + PAGE_SIZE
        allrecords = Shop_item.objects.all().order_by('-id')
        pageLength = (allrecords.count() + PAGE_SIZE -1)/ PAGE_SIZE
        shop_items = allrecords[startIndex:endIndex]
        shop_items = shop_items[:]
        for shop_item in shop_items:
            shop_item.tabId = shop_item.tabId
            shop_item.itemId = specials.getItemName(shop_item.itemId)
            shop_item.coinPrice = shop_item.coinPrice
            shop_item.silverPrice = shop_item.silverPrice
            shop_item.status = shop_item.status
            shop_item.isDiscount = shop_item.isDiscount
            shop_item.coinPirceDiscount = shop_item.coinPirceDiscount
            shop_item.silverPriceDiscount = shop_item.silverPriceDiscount
            shop_item.levelLimit = shop_item.levelLimit
            shop_item.vipLevelLimit = shop_item.vipLevelLimit
            shop_item.rank = shop_item.rank
            shop_item.shop_itemres = shop_item.shop_itemres
                
        _context = Context({
            'shop_items':shop_items,
            'pageIndex':pageIndex,
            'pageLength':pageLength
        })
    
        _template = loader.get_template('shop_item/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def getShop_itemJsonList(request):
    try:
        shop_items = Shop_item.objects.all().order_by('-id')
        shop_items = shop_items[:]
        for shop_item in shop_items:
            shop_item.tabId = shop_item.tabId
            shop_item.itemId = shop_item.itemId
            shop_item.coinPrice = shop_item.coinPrice
            shop_item.silverPrice = shop_item.silverPrice
            shop_item.status = shop_item.status
            shop_item.isDiscount = shop_item.isDiscount
            shop_item.coinPirceDiscount = shop_item.coinPirceDiscount
            shop_item.silverPriceDiscount = shop_item.silverPriceDiscount
            shop_item.levelLimit = shop_item.levelLimit
            shop_item.vipLevelLimit = shop_item.vipLevelLimit
            shop_item.rank = shop_item.rank
            shop_item.shop_itemres = shop_item.shop_itemres
                
        shop_itemJsonList = []
        for shop_item in shop_items:
            shop_itemJson = trans.shop_item2Json(shop_item)
            shop_itemJsonList.append(shop_itemJson)
        return HttpResponse(json.dumps({'code':1,'result':shop_itemJsonList}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def searchShop_item(request):
    try:
        condition = request.GET.get('condition')
        value = request.GET.get('value')
        searchArg = {condition:value}
        shop_items = Shop_item.objects.filter(**searchArg).all().order_by('-id')
        for shop_item in shop_items:
            shop_item.tabId = shop_item.tabId
            shop_item.itemId = shop_item.itemId
            shop_item.coinPrice = shop_item.coinPrice
            shop_item.silverPrice = shop_item.silverPrice
            shop_item.status = shop_item.status
            shop_item.isDiscount = shop_item.isDiscount
            shop_item.coinPirceDiscount = shop_item.coinPirceDiscount
            shop_item.silverPriceDiscount = shop_item.silverPriceDiscount
            shop_item.levelLimit = shop_item.levelLimit
            shop_item.vipLevelLimit = shop_item.vipLevelLimit
            shop_item.rank = shop_item.rank
            shop_item.shop_itemres = shop_item.shop_itemres
                
        _context = Context({
            'shop_items':shop_items,
            'pageLength':1
        })
    
        _template = loader.get_template('shop_item/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def addShop_item(request):
    try:
        shop_item = Shop_item()
        shop_item.deployId = 0;
        shop_item.tabId = int(request.POST.get('tabId'))        
        shop_item.itemId = request.POST.get('itemId')         
        shop_item.coinPrice = int(request.POST.get('coinPrice'))        
        shop_item.silverPrice = int(request.POST.get('silverPrice'))        
        shop_item.status = request.POST.get('status')         
        shop_item.isDiscount = request.POST.get('isDiscount')         
        shop_item.coinPirceDiscount = int(request.POST.get('coinPirceDiscount'))        
        shop_item.silverPriceDiscount = int(request.POST.get('silverPriceDiscount'))        
        shop_item.levelLimit = int(request.POST.get('levelLimit'))        
        shop_item.vipLevelLimit = int(request.POST.get('vipLevelLimit'))        
        shop_item.rank = int(request.POST.get('rank'))        
        shop_item.shop_itemres = request.POST.get('shop_itemres')                 
        shop_item.save()
        shop_item.deployId = shop_item.id + ModelTypes.Type_Shop_item * 1000000;
        shop_item.save()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')
    

def removeShop_item(request):
    try:
        shop_itemId = int(request.GET.get('shop_itemId'))
        shop_item = Shop_item.objects.get(id=shop_itemId)
        if shop_item :
            shop_item.delete()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def getShop_item(request):
    try:
        shop_itemId = int(request.GET.get('shop_itemId'))
        shop_item = Shop_item.objects.get(id=shop_itemId)
                
        shop_itemJson = trans.shop_item2Json(shop_item)
        
        if shop_item :
            return HttpResponse(json.dumps({'code':1,'result':shop_itemJson}), mimetype='application/json')
        else:
            return HttpResponse(json.dumps({'code':0,'message':'cannot find shop_item!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def editShop_item(request):
    try:
        shop_itemId = int(request.POST.get('shop_itemId'))
        shop_item = Shop_item.objects.get(id=shop_itemId)
        if shop_item :
            shop_item.tabId = int(request.POST.get('tabId'))
            
            shop_item.itemId = request.POST.get('itemId')
            shop_item.coinPrice = int(request.POST.get('coinPrice'))
            
            shop_item.silverPrice = int(request.POST.get('silverPrice'))
            
            shop_item.status = request.POST.get('status')
            shop_item.isDiscount = request.POST.get('isDiscount')
            shop_item.coinPirceDiscount = int(request.POST.get('coinPirceDiscount'))
            
            shop_item.silverPriceDiscount = int(request.POST.get('silverPriceDiscount'))
            
            shop_item.levelLimit = int(request.POST.get('levelLimit'))
            
            shop_item.vipLevelLimit = int(request.POST.get('vipLevelLimit'))
            
            shop_item.rank = int(request.POST.get('rank'))
            
            shop_item.shop_itemres = request.POST.get('shop_itemres')            
            shop_item.save()
            return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
        else :
            return HttpResponse(json.dumps({'code':0,'message':'cannot find shop_item!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

    
def getTavernList(request):
    try:
        pageIndex = int(request.GET.get("pageIndex",1))
        startIndex = (pageIndex -1)* PAGE_SIZE
        endIndex = startIndex + PAGE_SIZE
        allrecords = Tavern.objects.all().order_by('-id')
        pageLength = (allrecords.count() + PAGE_SIZE -1)/ PAGE_SIZE
        taverns = allrecords[startIndex:endIndex]
        taverns = taverns[:]
        for tavern in taverns:
            tavern.level = tavern.level
            tavern.requiredLevel = tavern.requiredLevel
            tavern.moneyType = tavern.moneyType
            tavern.money = tavern.money
            tavern.npcRates = tavern.npcRates
            tavern.taskRates = tavern.taskRates
                
        _context = Context({
            'taverns':taverns,
            'pageIndex':pageIndex,
            'pageLength':pageLength
        })
    
        _template = loader.get_template('tavern/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def getTavernJsonList(request):
    try:
        taverns = Tavern.objects.all().order_by('-id')
        taverns = taverns[:]
        for tavern in taverns:
            tavern.level = tavern.level
            tavern.requiredLevel = tavern.requiredLevel
            tavern.moneyType = tavern.moneyType
            tavern.money = tavern.money
            tavern.npcRates = tavern.npcRates
            tavern.taskRates = tavern.taskRates
                
        tavernJsonList = []
        for tavern in taverns:
            tavernJson = trans.tavern2Json(tavern)
            tavernJsonList.append(tavernJson)
        return HttpResponse(json.dumps({'code':1,'result':tavernJsonList}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def searchTavern(request):
    try:
        condition = request.GET.get('condition')
        value = request.GET.get('value')
        searchArg = {condition:value}
        taverns = Tavern.objects.filter(**searchArg).all().order_by('-id')
        for tavern in taverns:
            tavern.level = tavern.level
            tavern.requiredLevel = tavern.requiredLevel
            tavern.moneyType = tavern.moneyType
            tavern.money = tavern.money
            tavern.npcRates = tavern.npcRates
            tavern.taskRates = tavern.taskRates
                
        _context = Context({
            'taverns':taverns,
            'pageLength':1
        })
    
        _template = loader.get_template('tavern/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def addTavern(request):
    try:
        tavern = Tavern()
        tavern.deployId = 0;
        tavern.level = int(request.POST.get('level'))        
        tavern.requiredLevel = int(request.POST.get('requiredLevel'))        
        tavern.moneyType = request.POST.get('moneyType')         
        tavern.money = int(request.POST.get('money'))        
        tavern.npcRates = request.POST.get('npcRates')         
        tavern.taskRates = request.POST.get('taskRates')                 
        tavern.save()
        tavern.deployId = tavern.id + ModelTypes.Type_Tavern * 1000000;
        tavern.save()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')
    

def removeTavern(request):
    try:
        tavernId = int(request.GET.get('tavernId'))
        tavern = Tavern.objects.get(id=tavernId)
        if tavern :
            tavern.delete()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def getTavern(request):
    try:
        tavernId = int(request.GET.get('tavernId'))
        tavern = Tavern.objects.get(id=tavernId)
                
        tavernJson = trans.tavern2Json(tavern)
        
        if tavern :
            return HttpResponse(json.dumps({'code':1,'result':tavernJson}), mimetype='application/json')
        else:
            return HttpResponse(json.dumps({'code':0,'message':'cannot find tavern!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def editTavern(request):
    try:
        tavernId = int(request.POST.get('tavernId'))
        tavern = Tavern.objects.get(id=tavernId)
        if tavern :
            tavern.level = int(request.POST.get('level'))
            
            tavern.requiredLevel = int(request.POST.get('requiredLevel'))
            
            tavern.moneyType = request.POST.get('moneyType')
            tavern.money = int(request.POST.get('money'))
            
            tavern.npcRates = request.POST.get('npcRates')
            tavern.taskRates = request.POST.get('taskRates')            
            tavern.save()
            return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
        else :
            return HttpResponse(json.dumps({'code':0,'message':'cannot find tavern!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

    
def getVersus_battle_pointList(request):
    try:
        pageIndex = int(request.GET.get("pageIndex",1))
        startIndex = (pageIndex -1)* PAGE_SIZE
        endIndex = startIndex + PAGE_SIZE
        allrecords = Versus_battle_point.objects.all().order_by('-id')
        pageLength = (allrecords.count() + PAGE_SIZE -1)/ PAGE_SIZE
        versus_battle_points = allrecords[startIndex:endIndex]
        versus_battle_points = versus_battle_points[:]
        for versus_battle_point in versus_battle_points:
            versus_battle_point.level = versus_battle_point.level
            versus_battle_point.battlePointId = specials.getCampaignName(versus_battle_point.battlePointId)
                
        _context = Context({
            'versus_battle_points':versus_battle_points,
            'pageIndex':pageIndex,
            'pageLength':pageLength
        })
    
        _template = loader.get_template('versus_battle_point/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def getVersus_battle_pointJsonList(request):
    try:
        versus_battle_points = Versus_battle_point.objects.all().order_by('-id')
        versus_battle_points = versus_battle_points[:]
        for versus_battle_point in versus_battle_points:
            versus_battle_point.level = versus_battle_point.level
            versus_battle_point.battlePointId = versus_battle_point.battlePointId
                
        versus_battle_pointJsonList = []
        for versus_battle_point in versus_battle_points:
            versus_battle_pointJson = trans.versus_battle_point2Json(versus_battle_point)
            versus_battle_pointJsonList.append(versus_battle_pointJson)
        return HttpResponse(json.dumps({'code':1,'result':versus_battle_pointJsonList}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def searchVersus_battle_point(request):
    try:
        condition = request.GET.get('condition')
        value = request.GET.get('value')
        searchArg = {condition:value}
        versus_battle_points = Versus_battle_point.objects.filter(**searchArg).all().order_by('-id')
        for versus_battle_point in versus_battle_points:
            versus_battle_point.level = versus_battle_point.level
            versus_battle_point.battlePointId = versus_battle_point.battlePointId
                
        _context = Context({
            'versus_battle_points':versus_battle_points,
            'pageLength':1
        })
    
        _template = loader.get_template('versus_battle_point/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def addVersus_battle_point(request):
    try:
        versus_battle_point = Versus_battle_point()
        versus_battle_point.deployId = 0;
        versus_battle_point.level = int(request.POST.get('level'))        
        versus_battle_point.battlePointId = request.POST.get('battlePointId')                 
        versus_battle_point.save()
        versus_battle_point.deployId = versus_battle_point.id + ModelTypes.Type_Versus_battle_point * 1000000;
        versus_battle_point.save()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')
    

def removeVersus_battle_point(request):
    try:
        versus_battle_pointId = int(request.GET.get('versus_battle_pointId'))
        versus_battle_point = Versus_battle_point.objects.get(id=versus_battle_pointId)
        if versus_battle_point :
            versus_battle_point.delete()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def getVersus_battle_point(request):
    try:
        versus_battle_pointId = int(request.GET.get('versus_battle_pointId'))
        versus_battle_point = Versus_battle_point.objects.get(id=versus_battle_pointId)
                
        versus_battle_pointJson = trans.versus_battle_point2Json(versus_battle_point)
        
        if versus_battle_point :
            return HttpResponse(json.dumps({'code':1,'result':versus_battle_pointJson}), mimetype='application/json')
        else:
            return HttpResponse(json.dumps({'code':0,'message':'cannot find versus_battle_point!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def editVersus_battle_point(request):
    try:
        versus_battle_pointId = int(request.POST.get('versus_battle_pointId'))
        versus_battle_point = Versus_battle_point.objects.get(id=versus_battle_pointId)
        if versus_battle_point :
            versus_battle_point.level = int(request.POST.get('level'))
            
            versus_battle_point.battlePointId = request.POST.get('battlePointId')            
            versus_battle_point.save()
            return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
        else :
            return HttpResponse(json.dumps({'code':0,'message':'cannot find versus_battle_point!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

    
def getConstantsList(request):
    try:
        pageIndex = int(request.GET.get("pageIndex",1))
        startIndex = (pageIndex -1)* PAGE_SIZE
        endIndex = startIndex + PAGE_SIZE
        allrecords = Constants.objects.all().order_by('-id')
        pageLength = (allrecords.count() + PAGE_SIZE -1)/ PAGE_SIZE
        constantss = allrecords[startIndex:endIndex]
        constantss = constantss[:]
        for constants in constantss:
            constants.energeBoostPeriod = constants.energeBoostPeriod
            constants.energeBoostUnit = constants.energeBoostUnit
            constants.practiceDuration1 = constants.practiceDuration1
            constants.practiceDuration2 = constants.practiceDuration2
            constants.practiceDuration3 = constants.practiceDuration3
            constants.practiceSpeedExtraExp = constants.practiceSpeedExtraExp
            constants.practiceCostCoeff1 = constants.practiceCostCoeff1
            constants.practiceCostCoeff2 = constants.practiceCostCoeff2
            constants.practiceCostCoeff3 = constants.practiceCostCoeff3
            constants.practiceSpeedCost = constants.practiceSpeedCost
            constants.practiceDirectlyFinishCost = constants.practiceDirectlyFinishCost
            constants.practiceExpUnitCoeff1 = constants.practiceExpUnitCoeff1
            constants.practiceExpUnitCoeff2 = constants.practiceExpUnitCoeff2
            constants.practiceExpUnitCoeff3 = constants.practiceExpUnitCoeff3
            constants.practiceSpeedItemId = specials.getItemName(constants.practiceSpeedItemId)
            constants.versusItemId = specials.getItemName(constants.versusItemId)
            constants.versusLevelLimit = constants.versusLevelLimit
                
        _context = Context({
            'constantss':constantss,
            'pageIndex':pageIndex,
            'pageLength':pageLength
        })
    
        _template = loader.get_template('constants/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def getConstantsJsonList(request):
    try:
        constantss = Constants.objects.all().order_by('-id')
        constantss = constantss[:]
        for constants in constantss:
            constants.energeBoostPeriod = constants.energeBoostPeriod
            constants.energeBoostUnit = constants.energeBoostUnit
            constants.practiceDuration1 = constants.practiceDuration1
            constants.practiceDuration2 = constants.practiceDuration2
            constants.practiceDuration3 = constants.practiceDuration3
            constants.practiceSpeedExtraExp = constants.practiceSpeedExtraExp
            constants.practiceCostCoeff1 = constants.practiceCostCoeff1
            constants.practiceCostCoeff2 = constants.practiceCostCoeff2
            constants.practiceCostCoeff3 = constants.practiceCostCoeff3
            constants.practiceSpeedCost = constants.practiceSpeedCost
            constants.practiceDirectlyFinishCost = constants.practiceDirectlyFinishCost
            constants.practiceExpUnitCoeff1 = constants.practiceExpUnitCoeff1
            constants.practiceExpUnitCoeff2 = constants.practiceExpUnitCoeff2
            constants.practiceExpUnitCoeff3 = constants.practiceExpUnitCoeff3
            constants.practiceSpeedItemId = constants.practiceSpeedItemId
            constants.versusItemId = constants.versusItemId
            constants.versusLevelLimit = constants.versusLevelLimit
                
        constantsJsonList = []
        for constants in constantss:
            constantsJson = trans.constants2Json(constants)
            constantsJsonList.append(constantsJson)
        return HttpResponse(json.dumps({'code':1,'result':constantsJsonList}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def searchConstants(request):
    try:
        condition = request.GET.get('condition')
        value = request.GET.get('value')
        searchArg = {condition:value}
        constantss = Constants.objects.filter(**searchArg).all().order_by('-id')
        for constants in constantss:
            constants.energeBoostPeriod = constants.energeBoostPeriod
            constants.energeBoostUnit = constants.energeBoostUnit
            constants.practiceDuration1 = constants.practiceDuration1
            constants.practiceDuration2 = constants.practiceDuration2
            constants.practiceDuration3 = constants.practiceDuration3
            constants.practiceSpeedExtraExp = constants.practiceSpeedExtraExp
            constants.practiceCostCoeff1 = constants.practiceCostCoeff1
            constants.practiceCostCoeff2 = constants.practiceCostCoeff2
            constants.practiceCostCoeff3 = constants.practiceCostCoeff3
            constants.practiceSpeedCost = constants.practiceSpeedCost
            constants.practiceDirectlyFinishCost = constants.practiceDirectlyFinishCost
            constants.practiceExpUnitCoeff1 = constants.practiceExpUnitCoeff1
            constants.practiceExpUnitCoeff2 = constants.practiceExpUnitCoeff2
            constants.practiceExpUnitCoeff3 = constants.practiceExpUnitCoeff3
            constants.practiceSpeedItemId = constants.practiceSpeedItemId
            constants.versusItemId = constants.versusItemId
            constants.versusLevelLimit = constants.versusLevelLimit
                
        _context = Context({
            'constantss':constantss,
            'pageLength':1
        })
    
        _template = loader.get_template('constants/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def addConstants(request):
    try:
        constants = Constants()
        constants.deployId = 0;
        constants.energeBoostPeriod = int(request.POST.get('energeBoostPeriod'))        
        constants.energeBoostUnit = int(request.POST.get('energeBoostUnit'))        
        constants.practiceDuration1 = float(request.POST.get('practiceDuration1'))        
        constants.practiceDuration2 = int(request.POST.get('practiceDuration2'))        
        constants.practiceDuration3 = int(request.POST.get('practiceDuration3'))        
        constants.practiceSpeedExtraExp = int(request.POST.get('practiceSpeedExtraExp'))        
        constants.practiceCostCoeff1 = float(request.POST.get('practiceCostCoeff1'))        
        constants.practiceCostCoeff2 = float(request.POST.get('practiceCostCoeff2'))        
        constants.practiceCostCoeff3 = float(request.POST.get('practiceCostCoeff3'))        
        constants.practiceSpeedCost = int(request.POST.get('practiceSpeedCost'))        
        constants.practiceDirectlyFinishCost = int(request.POST.get('practiceDirectlyFinishCost'))        
        constants.practiceExpUnitCoeff1 = int(request.POST.get('practiceExpUnitCoeff1'))        
        constants.practiceExpUnitCoeff2 = int(request.POST.get('practiceExpUnitCoeff2'))        
        constants.practiceExpUnitCoeff3 = int(request.POST.get('practiceExpUnitCoeff3'))        
        constants.practiceSpeedItemId = request.POST.get('practiceSpeedItemId')         
        constants.versusItemId = request.POST.get('versusItemId')         
        constants.versusLevelLimit = int(request.POST.get('versusLevelLimit'))                
        constants.save()
        constants.deployId = constants.id + ModelTypes.Type_Constants * 1000000;
        constants.save()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')
    

def removeConstants(request):
    try:
        constantsId = int(request.GET.get('constantsId'))
        constants = Constants.objects.get(id=constantsId)
        if constants :
            constants.delete()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def getConstants(request):
    try:
        constantsId = int(request.GET.get('constantsId'))
        constants = Constants.objects.get(id=constantsId)
                
        constantsJson = trans.constants2Json(constants)
        
        if constants :
            return HttpResponse(json.dumps({'code':1,'result':constantsJson}), mimetype='application/json')
        else:
            return HttpResponse(json.dumps({'code':0,'message':'cannot find constants!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def editConstants(request):
    try:
        constantsId = int(request.POST.get('constantsId'))
        constants = Constants.objects.get(id=constantsId)
        if constants :
            constants.energeBoostPeriod = int(request.POST.get('energeBoostPeriod'))
            
            constants.energeBoostUnit = int(request.POST.get('energeBoostUnit'))
            
            constants.practiceDuration1 = float(request.POST.get('practiceDuration1'))
            
            constants.practiceDuration2 = int(request.POST.get('practiceDuration2'))
            
            constants.practiceDuration3 = int(request.POST.get('practiceDuration3'))
            
            constants.practiceSpeedExtraExp = int(request.POST.get('practiceSpeedExtraExp'))
            
            constants.practiceCostCoeff1 = float(request.POST.get('practiceCostCoeff1'))
            
            constants.practiceCostCoeff2 = float(request.POST.get('practiceCostCoeff2'))
            
            constants.practiceCostCoeff3 = float(request.POST.get('practiceCostCoeff3'))
            
            constants.practiceSpeedCost = int(request.POST.get('practiceSpeedCost'))
            
            constants.practiceDirectlyFinishCost = int(request.POST.get('practiceDirectlyFinishCost'))
            
            constants.practiceExpUnitCoeff1 = int(request.POST.get('practiceExpUnitCoeff1'))
            
            constants.practiceExpUnitCoeff2 = int(request.POST.get('practiceExpUnitCoeff2'))
            
            constants.practiceExpUnitCoeff3 = int(request.POST.get('practiceExpUnitCoeff3'))
            
            constants.practiceSpeedItemId = request.POST.get('practiceSpeedItemId')
            constants.versusItemId = request.POST.get('versusItemId')
            constants.versusLevelLimit = int(request.POST.get('versusLevelLimit'))
                        
            constants.save()
            return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
        else :
            return HttpResponse(json.dumps({'code':0,'message':'cannot find constants!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

    
def getForge_item_dataList(request):
    try:
        pageIndex = int(request.GET.get("pageIndex",1))
        startIndex = (pageIndex -1)* PAGE_SIZE
        endIndex = startIndex + PAGE_SIZE
        allrecords = Forge_item_data.objects.all().order_by('-id')
        pageLength = (allrecords.count() + PAGE_SIZE -1)/ PAGE_SIZE
        forge_item_datas = allrecords[startIndex:endIndex]
        forge_item_datas = forge_item_datas[:]
        for forge_item_data in forge_item_datas:
            forge_item_data.count = forge_item_data.count
            forge_item_data.min = forge_item_data.min
            forge_item_data.max = forge_item_data.max
            forge_item_data.gold = forge_item_data.gold
                
        _context = Context({
            'forge_item_datas':forge_item_datas,
            'pageIndex':pageIndex,
            'pageLength':pageLength
        })
    
        _template = loader.get_template('forge_item_data/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def getForge_item_dataJsonList(request):
    try:
        forge_item_datas = Forge_item_data.objects.all().order_by('-id')
        forge_item_datas = forge_item_datas[:]
        for forge_item_data in forge_item_datas:
            forge_item_data.count = forge_item_data.count
            forge_item_data.min = forge_item_data.min
            forge_item_data.max = forge_item_data.max
            forge_item_data.gold = forge_item_data.gold
                
        forge_item_dataJsonList = []
        for forge_item_data in forge_item_datas:
            forge_item_dataJson = trans.forge_item_data2Json(forge_item_data)
            forge_item_dataJsonList.append(forge_item_dataJson)
        return HttpResponse(json.dumps({'code':1,'result':forge_item_dataJsonList}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def searchForge_item_data(request):
    try:
        condition = request.GET.get('condition')
        value = request.GET.get('value')
        searchArg = {condition:value}
        forge_item_datas = Forge_item_data.objects.filter(**searchArg).all().order_by('-id')
        for forge_item_data in forge_item_datas:
            forge_item_data.count = forge_item_data.count
            forge_item_data.min = forge_item_data.min
            forge_item_data.max = forge_item_data.max
            forge_item_data.gold = forge_item_data.gold
                
        _context = Context({
            'forge_item_datas':forge_item_datas,
            'pageLength':1
        })
    
        _template = loader.get_template('forge_item_data/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def addForge_item_data(request):
    try:
        forge_item_data = Forge_item_data()
        forge_item_data.deployId = 0;
        forge_item_data.count = int(request.POST.get('count'))        
        forge_item_data.min = int(request.POST.get('min'))        
        forge_item_data.max = int(request.POST.get('max'))        
        forge_item_data.gold = int(request.POST.get('gold'))                
        forge_item_data.save()
        forge_item_data.deployId = forge_item_data.id + ModelTypes.Type_Forge_item_data * 1000000;
        forge_item_data.save()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')
    

def removeForge_item_data(request):
    try:
        forge_item_dataId = int(request.GET.get('forge_item_dataId'))
        forge_item_data = Forge_item_data.objects.get(id=forge_item_dataId)
        if forge_item_data :
            forge_item_data.delete()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def getForge_item_data(request):
    try:
        forge_item_dataId = int(request.GET.get('forge_item_dataId'))
        forge_item_data = Forge_item_data.objects.get(id=forge_item_dataId)
                
        forge_item_dataJson = trans.forge_item_data2Json(forge_item_data)
        
        if forge_item_data :
            return HttpResponse(json.dumps({'code':1,'result':forge_item_dataJson}), mimetype='application/json')
        else:
            return HttpResponse(json.dumps({'code':0,'message':'cannot find forge_item_data!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def editForge_item_data(request):
    try:
        forge_item_dataId = int(request.POST.get('forge_item_dataId'))
        forge_item_data = Forge_item_data.objects.get(id=forge_item_dataId)
        if forge_item_data :
            forge_item_data.count = int(request.POST.get('count'))
            
            forge_item_data.min = int(request.POST.get('min'))
            
            forge_item_data.max = int(request.POST.get('max'))
            
            forge_item_data.gold = int(request.POST.get('gold'))
                        
            forge_item_data.save()
            return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
        else :
            return HttpResponse(json.dumps({'code':0,'message':'cannot find forge_item_data!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

    
def getItem_fragmentList(request):
    try:
        pageIndex = int(request.GET.get("pageIndex",1))
        startIndex = (pageIndex -1)* PAGE_SIZE
        endIndex = startIndex + PAGE_SIZE
        allrecords = Item_fragment.objects.all().order_by('-id')
        pageLength = (allrecords.count() + PAGE_SIZE -1)/ PAGE_SIZE
        item_fragments = allrecords[startIndex:endIndex]
        item_fragments = item_fragments[:]
        for item_fragment in item_fragments:
            item_fragment.itemId = specials.getItemName(item_fragment.itemId)
            item_fragment.type = item_fragment.type
            item_fragment.fragments = item_fragment.fragments
                
        _context = Context({
            'item_fragments':item_fragments,
            'pageIndex':pageIndex,
            'pageLength':pageLength
        })
    
        _template = loader.get_template('item_fragment/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def getItem_fragmentJsonList(request):
    try:
        item_fragments = Item_fragment.objects.all().order_by('-id')
        item_fragments = item_fragments[:]
        for item_fragment in item_fragments:
            item_fragment.itemId = item_fragment.itemId
            item_fragment.type = item_fragment.type
            item_fragment.fragments = item_fragment.fragments
                
        item_fragmentJsonList = []
        for item_fragment in item_fragments:
            item_fragmentJson = trans.item_fragment2Json(item_fragment)
            item_fragmentJsonList.append(item_fragmentJson)
        return HttpResponse(json.dumps({'code':1,'result':item_fragmentJsonList}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def searchItem_fragment(request):
    try:
        condition = request.GET.get('condition')
        value = request.GET.get('value')
        searchArg = {condition:value}
        item_fragments = Item_fragment.objects.filter(**searchArg).all().order_by('-id')
        for item_fragment in item_fragments:
            item_fragment.itemId = item_fragment.itemId
            item_fragment.type = item_fragment.type
            item_fragment.fragments = item_fragment.fragments
                
        _context = Context({
            'item_fragments':item_fragments,
            'pageLength':1
        })
    
        _template = loader.get_template('item_fragment/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def addItem_fragment(request):
    try:
        item_fragment = Item_fragment()
        item_fragment.deployId = 0;
        item_fragment.itemId = request.POST.get('itemId')         
        item_fragment.type = request.POST.get('type')         
        item_fragment.fragments = request.POST.get('fragments')                 
        item_fragment.save()
        item_fragment.deployId = item_fragment.id + ModelTypes.Type_Item_fragment * 1000000;
        item_fragment.save()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')
    

def removeItem_fragment(request):
    try:
        item_fragmentId = int(request.GET.get('item_fragmentId'))
        item_fragment = Item_fragment.objects.get(id=item_fragmentId)
        if item_fragment :
            item_fragment.delete()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def getItem_fragment(request):
    try:
        item_fragmentId = int(request.GET.get('item_fragmentId'))
        item_fragment = Item_fragment.objects.get(id=item_fragmentId)
                
        item_fragmentJson = trans.item_fragment2Json(item_fragment)
        
        if item_fragment :
            return HttpResponse(json.dumps({'code':1,'result':item_fragmentJson}), mimetype='application/json')
        else:
            return HttpResponse(json.dumps({'code':0,'message':'cannot find item_fragment!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def editItem_fragment(request):
    try:
        item_fragmentId = int(request.POST.get('item_fragmentId'))
        item_fragment = Item_fragment.objects.get(id=item_fragmentId)
        if item_fragment :
            item_fragment.itemId = request.POST.get('itemId')
            item_fragment.type = request.POST.get('type')
            item_fragment.fragments = request.POST.get('fragments')            
            item_fragment.save()
            return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
        else :
            return HttpResponse(json.dumps({'code':0,'message':'cannot find item_fragment!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

    
def getItem_extra_propList(request):
    try:
        pageIndex = int(request.GET.get("pageIndex",1))
        startIndex = (pageIndex -1)* PAGE_SIZE
        endIndex = startIndex + PAGE_SIZE
        allrecords = Item_extra_prop.objects.all().order_by('-id')
        pageLength = (allrecords.count() + PAGE_SIZE -1)/ PAGE_SIZE
        item_extra_props = allrecords[startIndex:endIndex]
        item_extra_props = item_extra_props[:]
        for item_extra_prop in item_extra_props:
            item_extra_prop.level = item_extra_prop.level
            item_extra_prop.property = item_extra_prop.property
            item_extra_prop.valueType = item_extra_prop.valueType
            item_extra_prop.min = item_extra_prop.min
            item_extra_prop.max = item_extra_prop.max
                
        _context = Context({
            'item_extra_props':item_extra_props,
            'pageIndex':pageIndex,
            'pageLength':pageLength
        })
    
        _template = loader.get_template('item_extra_prop/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def getItem_extra_propJsonList(request):
    try:
        item_extra_props = Item_extra_prop.objects.all().order_by('-id')
        item_extra_props = item_extra_props[:]
        for item_extra_prop in item_extra_props:
            item_extra_prop.level = item_extra_prop.level
            item_extra_prop.property = item_extra_prop.property
            item_extra_prop.valueType = item_extra_prop.valueType
            item_extra_prop.min = item_extra_prop.min
            item_extra_prop.max = item_extra_prop.max
                
        item_extra_propJsonList = []
        for item_extra_prop in item_extra_props:
            item_extra_propJson = trans.item_extra_prop2Json(item_extra_prop)
            item_extra_propJsonList.append(item_extra_propJson)
        return HttpResponse(json.dumps({'code':1,'result':item_extra_propJsonList}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def searchItem_extra_prop(request):
    try:
        condition = request.GET.get('condition')
        value = request.GET.get('value')
        searchArg = {condition:value}
        item_extra_props = Item_extra_prop.objects.filter(**searchArg).all().order_by('-id')
        for item_extra_prop in item_extra_props:
            item_extra_prop.level = item_extra_prop.level
            item_extra_prop.property = item_extra_prop.property
            item_extra_prop.valueType = item_extra_prop.valueType
            item_extra_prop.min = item_extra_prop.min
            item_extra_prop.max = item_extra_prop.max
                
        _context = Context({
            'item_extra_props':item_extra_props,
            'pageLength':1
        })
    
        _template = loader.get_template('item_extra_prop/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def addItem_extra_prop(request):
    try:
        item_extra_prop = Item_extra_prop()
        item_extra_prop.deployId = 0;
        item_extra_prop.level = int(request.POST.get('level'))        
        item_extra_prop.property = request.POST.get('property')         
        item_extra_prop.valueType = request.POST.get('valueType')         
        item_extra_prop.min = int(request.POST.get('min'))        
        item_extra_prop.max = int(request.POST.get('max'))                
        item_extra_prop.save()
        item_extra_prop.deployId = item_extra_prop.id + ModelTypes.Type_Item_extra_prop * 1000000;
        item_extra_prop.save()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')
    

def removeItem_extra_prop(request):
    try:
        item_extra_propId = int(request.GET.get('item_extra_propId'))
        item_extra_prop = Item_extra_prop.objects.get(id=item_extra_propId)
        if item_extra_prop :
            item_extra_prop.delete()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def getItem_extra_prop(request):
    try:
        item_extra_propId = int(request.GET.get('item_extra_propId'))
        item_extra_prop = Item_extra_prop.objects.get(id=item_extra_propId)
                
        item_extra_propJson = trans.item_extra_prop2Json(item_extra_prop)
        
        if item_extra_prop :
            return HttpResponse(json.dumps({'code':1,'result':item_extra_propJson}), mimetype='application/json')
        else:
            return HttpResponse(json.dumps({'code':0,'message':'cannot find item_extra_prop!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def editItem_extra_prop(request):
    try:
        item_extra_propId = int(request.POST.get('item_extra_propId'))
        item_extra_prop = Item_extra_prop.objects.get(id=item_extra_propId)
        if item_extra_prop :
            item_extra_prop.level = int(request.POST.get('level'))
            
            item_extra_prop.property = request.POST.get('property')
            item_extra_prop.valueType = request.POST.get('valueType')
            item_extra_prop.min = int(request.POST.get('min'))
            
            item_extra_prop.max = int(request.POST.get('max'))
                        
            item_extra_prop.save()
            return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
        else :
            return HttpResponse(json.dumps({'code':0,'message':'cannot find item_extra_prop!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

    
def getItem_perfect_prop_dataList(request):
    try:
        pageIndex = int(request.GET.get("pageIndex",1))
        startIndex = (pageIndex -1)* PAGE_SIZE
        endIndex = startIndex + PAGE_SIZE
        allrecords = Item_perfect_prop_data.objects.all().order_by('-id')
        pageLength = (allrecords.count() + PAGE_SIZE -1)/ PAGE_SIZE
        item_perfect_prop_datas = allrecords[startIndex:endIndex]
        item_perfect_prop_datas = item_perfect_prop_datas[:]
        for item_perfect_prop_data in item_perfect_prop_datas:
            item_perfect_prop_data.itemQuality = item_perfect_prop_data.itemQuality
            item_perfect_prop_data.type = item_perfect_prop_data.type
            item_perfect_prop_data.prop = item_perfect_prop_data.prop
            item_perfect_prop_data.min = item_perfect_prop_data.min
            item_perfect_prop_data.max = item_perfect_prop_data.max
                
        _context = Context({
            'item_perfect_prop_datas':item_perfect_prop_datas,
            'pageIndex':pageIndex,
            'pageLength':pageLength
        })
    
        _template = loader.get_template('item_perfect_prop_data/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def getItem_perfect_prop_dataJsonList(request):
    try:
        item_perfect_prop_datas = Item_perfect_prop_data.objects.all().order_by('-id')
        item_perfect_prop_datas = item_perfect_prop_datas[:]
        for item_perfect_prop_data in item_perfect_prop_datas:
            item_perfect_prop_data.itemQuality = item_perfect_prop_data.itemQuality
            item_perfect_prop_data.type = item_perfect_prop_data.type
            item_perfect_prop_data.prop = item_perfect_prop_data.prop
            item_perfect_prop_data.min = item_perfect_prop_data.min
            item_perfect_prop_data.max = item_perfect_prop_data.max
                
        item_perfect_prop_dataJsonList = []
        for item_perfect_prop_data in item_perfect_prop_datas:
            item_perfect_prop_dataJson = trans.item_perfect_prop_data2Json(item_perfect_prop_data)
            item_perfect_prop_dataJsonList.append(item_perfect_prop_dataJson)
        return HttpResponse(json.dumps({'code':1,'result':item_perfect_prop_dataJsonList}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def searchItem_perfect_prop_data(request):
    try:
        condition = request.GET.get('condition')
        value = request.GET.get('value')
        searchArg = {condition:value}
        item_perfect_prop_datas = Item_perfect_prop_data.objects.filter(**searchArg).all().order_by('-id')
        for item_perfect_prop_data in item_perfect_prop_datas:
            item_perfect_prop_data.itemQuality = item_perfect_prop_data.itemQuality
            item_perfect_prop_data.type = item_perfect_prop_data.type
            item_perfect_prop_data.prop = item_perfect_prop_data.prop
            item_perfect_prop_data.min = item_perfect_prop_data.min
            item_perfect_prop_data.max = item_perfect_prop_data.max
                
        _context = Context({
            'item_perfect_prop_datas':item_perfect_prop_datas,
            'pageLength':1
        })
    
        _template = loader.get_template('item_perfect_prop_data/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def addItem_perfect_prop_data(request):
    try:
        item_perfect_prop_data = Item_perfect_prop_data()
        item_perfect_prop_data.deployId = 0;
        item_perfect_prop_data.itemQuality = request.POST.get('itemQuality')         
        item_perfect_prop_data.type = request.POST.get('type')         
        item_perfect_prop_data.prop = request.POST.get('prop')         
        item_perfect_prop_data.min = int(request.POST.get('min'))        
        item_perfect_prop_data.max = int(request.POST.get('max'))                
        item_perfect_prop_data.save()
        item_perfect_prop_data.deployId = item_perfect_prop_data.id + ModelTypes.Type_Item_perfect_prop_data * 1000000;
        item_perfect_prop_data.save()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')
    

def removeItem_perfect_prop_data(request):
    try:
        item_perfect_prop_dataId = int(request.GET.get('item_perfect_prop_dataId'))
        item_perfect_prop_data = Item_perfect_prop_data.objects.get(id=item_perfect_prop_dataId)
        if item_perfect_prop_data :
            item_perfect_prop_data.delete()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def getItem_perfect_prop_data(request):
    try:
        item_perfect_prop_dataId = int(request.GET.get('item_perfect_prop_dataId'))
        item_perfect_prop_data = Item_perfect_prop_data.objects.get(id=item_perfect_prop_dataId)
                
        item_perfect_prop_dataJson = trans.item_perfect_prop_data2Json(item_perfect_prop_data)
        
        if item_perfect_prop_data :
            return HttpResponse(json.dumps({'code':1,'result':item_perfect_prop_dataJson}), mimetype='application/json')
        else:
            return HttpResponse(json.dumps({'code':0,'message':'cannot find item_perfect_prop_data!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def editItem_perfect_prop_data(request):
    try:
        item_perfect_prop_dataId = int(request.POST.get('item_perfect_prop_dataId'))
        item_perfect_prop_data = Item_perfect_prop_data.objects.get(id=item_perfect_prop_dataId)
        if item_perfect_prop_data :
            item_perfect_prop_data.itemQuality = request.POST.get('itemQuality')
            item_perfect_prop_data.type = request.POST.get('type')
            item_perfect_prop_data.prop = request.POST.get('prop')
            item_perfect_prop_data.min = int(request.POST.get('min'))
            
            item_perfect_prop_data.max = int(request.POST.get('max'))
                        
            item_perfect_prop_data.save()
            return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
        else :
            return HttpResponse(json.dumps({'code':0,'message':'cannot find item_perfect_prop_data!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

    
def getHomeList(request):
    try:
        pageIndex = int(request.GET.get("pageIndex",1))
        startIndex = (pageIndex -1)* PAGE_SIZE
        endIndex = startIndex + PAGE_SIZE
        allrecords = Home.objects.all().order_by('-id')
        pageLength = (allrecords.count() + PAGE_SIZE -1)/ PAGE_SIZE
        homes = allrecords[startIndex:endIndex]
        homes = homes[:]
        for home in homes:
            home.homeId = home.homeId
            home.cellId = home.cellId
            home.level = home.level
            home.cardId = home.cardId
            home.taskId = home.taskId
            home.homeres = home.homeres
                
        _context = Context({
            'homes':homes,
            'pageIndex':pageIndex,
            'pageLength':pageLength
        })
    
        _template = loader.get_template('home/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def getHomeJsonList(request):
    try:
        homes = Home.objects.all().order_by('-id')
        homes = homes[:]
        for home in homes:
            home.homeId = home.homeId
            home.cellId = home.cellId
            home.level = home.level
            home.cardId = home.cardId
            home.taskId = home.taskId
            home.homeres = home.homeres
                
        homeJsonList = []
        for home in homes:
            homeJson = trans.home2Json(home)
            homeJsonList.append(homeJson)
        return HttpResponse(json.dumps({'code':1,'result':homeJsonList}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def searchHome(request):
    try:
        condition = request.GET.get('condition')
        value = request.GET.get('value')
        searchArg = {condition:value}
        homes = Home.objects.filter(**searchArg).all().order_by('-id')
        for home in homes:
            home.homeId = home.homeId
            home.cellId = home.cellId
            home.level = home.level
            home.cardId = home.cardId
            home.taskId = home.taskId
            home.homeres = home.homeres
                
        _context = Context({
            'homes':homes,
            'pageLength':1
        })
    
        _template = loader.get_template('home/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def addHome(request):
    try:
        home = Home()
        home.deployId = 0;
        home.homeId = int(request.POST.get('homeId'))        
        home.cellId = int(request.POST.get('cellId'))        
        home.level = int(request.POST.get('level'))        
        home.cardId = request.POST.get('cardId')         
        home.taskId = request.POST.get('taskId')         
        home.homeres = request.POST.get('homeres')                 
        home.save()
        home.deployId = home.id + ModelTypes.Type_Home * 1000000;
        home.save()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')
    

def removeHome(request):
    try:
        homeId = int(request.GET.get('homeId'))
        home = Home.objects.get(id=homeId)
        if home :
            home.delete()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def getHome(request):
    try:
        homeId = int(request.GET.get('homeId'))
        home = Home.objects.get(id=homeId)
                
        homeJson = trans.home2Json(home)
        
        if home :
            return HttpResponse(json.dumps({'code':1,'result':homeJson}), mimetype='application/json')
        else:
            return HttpResponse(json.dumps({'code':0,'message':'cannot find home!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def editHome(request):
    try:
        homeId = int(request.POST.get('homeId'))
        home = Home.objects.get(id=homeId)
        if home :
            home.homeId = int(request.POST.get('homeId'))
            
            home.cellId = int(request.POST.get('cellId'))
            
            home.level = int(request.POST.get('level'))
            
            home.cardId = request.POST.get('cardId')
            home.taskId = request.POST.get('taskId')
            home.homeres = request.POST.get('homeres')            
            home.save()
            return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
        else :
            return HttpResponse(json.dumps({'code':0,'message':'cannot find home!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

    
def getOpen_bagList(request):
    try:
        pageIndex = int(request.GET.get("pageIndex",1))
        startIndex = (pageIndex -1)* PAGE_SIZE
        endIndex = startIndex + PAGE_SIZE
        allrecords = Open_bag.objects.all().order_by('-id')
        pageLength = (allrecords.count() + PAGE_SIZE -1)/ PAGE_SIZE
        open_bags = allrecords[startIndex:endIndex]
        open_bags = open_bags[:]
        for open_bag in open_bags:
            open_bag.bagCount = open_bag.bagCount
            open_bag.gold = open_bag.gold
                
        _context = Context({
            'open_bags':open_bags,
            'pageIndex':pageIndex,
            'pageLength':pageLength
        })
    
        _template = loader.get_template('open_bag/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def getOpen_bagJsonList(request):
    try:
        open_bags = Open_bag.objects.all().order_by('-id')
        open_bags = open_bags[:]
        for open_bag in open_bags:
            open_bag.bagCount = open_bag.bagCount
            open_bag.gold = open_bag.gold
                
        open_bagJsonList = []
        for open_bag in open_bags:
            open_bagJson = trans.open_bag2Json(open_bag)
            open_bagJsonList.append(open_bagJson)
        return HttpResponse(json.dumps({'code':1,'result':open_bagJsonList}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def searchOpen_bag(request):
    try:
        condition = request.GET.get('condition')
        value = request.GET.get('value')
        searchArg = {condition:value}
        open_bags = Open_bag.objects.filter(**searchArg).all().order_by('-id')
        for open_bag in open_bags:
            open_bag.bagCount = open_bag.bagCount
            open_bag.gold = open_bag.gold
                
        _context = Context({
            'open_bags':open_bags,
            'pageLength':1
        })
    
        _template = loader.get_template('open_bag/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def addOpen_bag(request):
    try:
        open_bag = Open_bag()
        open_bag.deployId = 0;
        open_bag.bagCount = int(request.POST.get('bagCount'))        
        open_bag.gold = int(request.POST.get('gold'))                
        open_bag.save()
        open_bag.deployId = open_bag.id + ModelTypes.Type_Open_bag * 1000000;
        open_bag.save()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')
    

def removeOpen_bag(request):
    try:
        open_bagId = int(request.GET.get('open_bagId'))
        open_bag = Open_bag.objects.get(id=open_bagId)
        if open_bag :
            open_bag.delete()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def getOpen_bag(request):
    try:
        open_bagId = int(request.GET.get('open_bagId'))
        open_bag = Open_bag.objects.get(id=open_bagId)
                
        open_bagJson = trans.open_bag2Json(open_bag)
        
        if open_bag :
            return HttpResponse(json.dumps({'code':1,'result':open_bagJson}), mimetype='application/json')
        else:
            return HttpResponse(json.dumps({'code':0,'message':'cannot find open_bag!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def editOpen_bag(request):
    try:
        open_bagId = int(request.POST.get('open_bagId'))
        open_bag = Open_bag.objects.get(id=open_bagId)
        if open_bag :
            open_bag.bagCount = int(request.POST.get('bagCount'))
            
            open_bag.gold = int(request.POST.get('gold'))
                        
            open_bag.save()
            return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
        else :
            return HttpResponse(json.dumps({'code':0,'message':'cannot find open_bag!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

    
def getSilver_bagList(request):
    try:
        pageIndex = int(request.GET.get("pageIndex",1))
        startIndex = (pageIndex -1)* PAGE_SIZE
        endIndex = startIndex + PAGE_SIZE
        allrecords = Silver_bag.objects.all().order_by('-id')
        pageLength = (allrecords.count() + PAGE_SIZE -1)/ PAGE_SIZE
        silver_bags = allrecords[startIndex:endIndex]
        silver_bags = silver_bags[:]
        for silver_bag in silver_bags:
            silver_bag.itemId = specials.getItemName(silver_bag.itemId)
            silver_bag.silvers = silver_bag.silvers
                
        _context = Context({
            'silver_bags':silver_bags,
            'pageIndex':pageIndex,
            'pageLength':pageLength
        })
    
        _template = loader.get_template('silver_bag/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def getSilver_bagJsonList(request):
    try:
        silver_bags = Silver_bag.objects.all().order_by('-id')
        silver_bags = silver_bags[:]
        for silver_bag in silver_bags:
            silver_bag.itemId = silver_bag.itemId
            silver_bag.silvers = silver_bag.silvers
                
        silver_bagJsonList = []
        for silver_bag in silver_bags:
            silver_bagJson = trans.silver_bag2Json(silver_bag)
            silver_bagJsonList.append(silver_bagJson)
        return HttpResponse(json.dumps({'code':1,'result':silver_bagJsonList}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def searchSilver_bag(request):
    try:
        condition = request.GET.get('condition')
        value = request.GET.get('value')
        searchArg = {condition:value}
        silver_bags = Silver_bag.objects.filter(**searchArg).all().order_by('-id')
        for silver_bag in silver_bags:
            silver_bag.itemId = silver_bag.itemId
            silver_bag.silvers = silver_bag.silvers
                
        _context = Context({
            'silver_bags':silver_bags,
            'pageLength':1
        })
    
        _template = loader.get_template('silver_bag/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def addSilver_bag(request):
    try:
        silver_bag = Silver_bag()
        silver_bag.deployId = 0;
        silver_bag.itemId = request.POST.get('itemId')         
        silver_bag.silvers = request.POST.get('silvers')                 
        silver_bag.save()
        silver_bag.deployId = silver_bag.id + ModelTypes.Type_Silver_bag * 1000000;
        silver_bag.save()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')
    

def removeSilver_bag(request):
    try:
        silver_bagId = int(request.GET.get('silver_bagId'))
        silver_bag = Silver_bag.objects.get(id=silver_bagId)
        if silver_bag :
            silver_bag.delete()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def getSilver_bag(request):
    try:
        silver_bagId = int(request.GET.get('silver_bagId'))
        silver_bag = Silver_bag.objects.get(id=silver_bagId)
                
        silver_bagJson = trans.silver_bag2Json(silver_bag)
        
        if silver_bag :
            return HttpResponse(json.dumps({'code':1,'result':silver_bagJson}), mimetype='application/json')
        else:
            return HttpResponse(json.dumps({'code':0,'message':'cannot find silver_bag!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def editSilver_bag(request):
    try:
        silver_bagId = int(request.POST.get('silver_bagId'))
        silver_bag = Silver_bag.objects.get(id=silver_bagId)
        if silver_bag :
            silver_bag.itemId = request.POST.get('itemId')
            silver_bag.silvers = request.POST.get('silvers')            
            silver_bag.save()
            return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
        else :
            return HttpResponse(json.dumps({'code':0,'message':'cannot find silver_bag!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

    
def getGift_bagList(request):
    try:
        pageIndex = int(request.GET.get("pageIndex",1))
        startIndex = (pageIndex -1)* PAGE_SIZE
        endIndex = startIndex + PAGE_SIZE
        allrecords = Gift_bag.objects.all().order_by('-id')
        pageLength = (allrecords.count() + PAGE_SIZE -1)/ PAGE_SIZE
        gift_bags = allrecords[startIndex:endIndex]
        gift_bags = gift_bags[:]
        for gift_bag in gift_bags:
            gift_bag.itemId = specials.getItemName(gift_bag.itemId)
            gift_bag.reward = gift_bag.reward
                
        _context = Context({
            'gift_bags':gift_bags,
            'pageIndex':pageIndex,
            'pageLength':pageLength
        })
    
        _template = loader.get_template('gift_bag/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def getGift_bagJsonList(request):
    try:
        gift_bags = Gift_bag.objects.all().order_by('-id')
        gift_bags = gift_bags[:]
        for gift_bag in gift_bags:
            gift_bag.itemId = gift_bag.itemId
            gift_bag.reward = gift_bag.reward
                
        gift_bagJsonList = []
        for gift_bag in gift_bags:
            gift_bagJson = trans.gift_bag2Json(gift_bag)
            gift_bagJsonList.append(gift_bagJson)
        return HttpResponse(json.dumps({'code':1,'result':gift_bagJsonList}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def searchGift_bag(request):
    try:
        condition = request.GET.get('condition')
        value = request.GET.get('value')
        searchArg = {condition:value}
        gift_bags = Gift_bag.objects.filter(**searchArg).all().order_by('-id')
        for gift_bag in gift_bags:
            gift_bag.itemId = gift_bag.itemId
            gift_bag.reward = gift_bag.reward
                
        _context = Context({
            'gift_bags':gift_bags,
            'pageLength':1
        })
    
        _template = loader.get_template('gift_bag/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def addGift_bag(request):
    try:
        gift_bag = Gift_bag()
        gift_bag.deployId = 0;
        gift_bag.itemId = request.POST.get('itemId')         
        gift_bag.reward = request.POST.get('reward')                 
        gift_bag.save()
        gift_bag.deployId = gift_bag.id + ModelTypes.Type_Gift_bag * 1000000;
        gift_bag.save()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')
    

def removeGift_bag(request):
    try:
        gift_bagId = int(request.GET.get('gift_bagId'))
        gift_bag = Gift_bag.objects.get(id=gift_bagId)
        if gift_bag :
            gift_bag.delete()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def getGift_bag(request):
    try:
        gift_bagId = int(request.GET.get('gift_bagId'))
        gift_bag = Gift_bag.objects.get(id=gift_bagId)
                
        gift_bagJson = trans.gift_bag2Json(gift_bag)
        
        if gift_bag :
            return HttpResponse(json.dumps({'code':1,'result':gift_bagJson}), mimetype='application/json')
        else:
            return HttpResponse(json.dumps({'code':0,'message':'cannot find gift_bag!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def editGift_bag(request):
    try:
        gift_bagId = int(request.POST.get('gift_bagId'))
        gift_bag = Gift_bag.objects.get(id=gift_bagId)
        if gift_bag :
            gift_bag.itemId = request.POST.get('itemId')
            gift_bag.reward = request.POST.get('reward')            
            gift_bag.save()
            return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
        else :
            return HttpResponse(json.dumps({'code':0,'message':'cannot find gift_bag!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

    
def getTreasureList(request):
    try:
        pageIndex = int(request.GET.get("pageIndex",1))
        startIndex = (pageIndex -1)* PAGE_SIZE
        endIndex = startIndex + PAGE_SIZE
        allrecords = Treasure.objects.all().order_by('-id')
        pageLength = (allrecords.count() + PAGE_SIZE -1)/ PAGE_SIZE
        treasures = allrecords[startIndex:endIndex]
        treasures = treasures[:]
        for treasure in treasures:
            treasure.itemId = specials.getItemName(treasure.itemId)
            treasure.items = treasure.items
                
        _context = Context({
            'treasures':treasures,
            'pageIndex':pageIndex,
            'pageLength':pageLength
        })
    
        _template = loader.get_template('treasure/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def getTreasureJsonList(request):
    try:
        treasures = Treasure.objects.all().order_by('-id')
        treasures = treasures[:]
        for treasure in treasures:
            treasure.itemId = treasure.itemId
            treasure.items = treasure.items
                
        treasureJsonList = []
        for treasure in treasures:
            treasureJson = trans.treasure2Json(treasure)
            treasureJsonList.append(treasureJson)
        return HttpResponse(json.dumps({'code':1,'result':treasureJsonList}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def searchTreasure(request):
    try:
        condition = request.GET.get('condition')
        value = request.GET.get('value')
        searchArg = {condition:value}
        treasures = Treasure.objects.filter(**searchArg).all().order_by('-id')
        for treasure in treasures:
            treasure.itemId = treasure.itemId
            treasure.items = treasure.items
                
        _context = Context({
            'treasures':treasures,
            'pageLength':1
        })
    
        _template = loader.get_template('treasure/list.tpl')
        _output = _template.render(_context)
        return HttpResponse(_output)
    except:
        HttpResponse(json.dumps({'code':1,'message':traceback.format_exc()}), mimetype='application/json')

def addTreasure(request):
    try:
        treasure = Treasure()
        treasure.deployId = 0;
        treasure.itemId = request.POST.get('itemId')         
        treasure.items = request.POST.get('items')                 
        treasure.save()
        treasure.deployId = treasure.id + ModelTypes.Type_Treasure * 1000000;
        treasure.save()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')
    

def removeTreasure(request):
    try:
        treasureId = int(request.GET.get('treasureId'))
        treasure = Treasure.objects.get(id=treasureId)
        if treasure :
            treasure.delete()
        return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def getTreasure(request):
    try:
        treasureId = int(request.GET.get('treasureId'))
        treasure = Treasure.objects.get(id=treasureId)
                
        treasureJson = trans.treasure2Json(treasure)
        
        if treasure :
            return HttpResponse(json.dumps({'code':1,'result':treasureJson}), mimetype='application/json')
        else:
            return HttpResponse(json.dumps({'code':0,'message':'cannot find treasure!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

def editTreasure(request):
    try:
        treasureId = int(request.POST.get('treasureId'))
        treasure = Treasure.objects.get(id=treasureId)
        if treasure :
            treasure.itemId = request.POST.get('itemId')
            treasure.items = request.POST.get('items')            
            treasure.save()
            return HttpResponse(json.dumps({'code':1}), mimetype='application/json')
        else :
            return HttpResponse(json.dumps({'code':0,'message':'cannot find treasure!'}), mimetype='application/json')
    except Exception:
        return HttpResponse(json.dumps({'code':0,'message':traceback.format_exc()}), mimetype='application/json')

    